Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to know next primary key value of table without inserting record in sql server?

I have a things in my project where i need to display next primary key in field without inserting the record physically?

How can i know the next value of primary key without inserting record?

i.e. Lets have 10 records with ID columns as primary key. Now I have deleted 10th record, so the next value will be 11.

I want to know the value of next primary key (11 in my case) without inserting the record physically in the table.

In short, the future next value of the primary key.

How can i get that??

Please provide the solution.

like image 496
amit patel Avatar asked Dec 23 '11 05:12

amit patel


People also ask

How to get the primary key of a table in SQL?

Here's another way from the question get table primary key using sql query: SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY (OBJECT_ID (CONSTRAINT_SCHEMA+'.'+CONSTRAINT_NAME), 'IsPrimaryKey') = 1 AND TABLE_NAME = '<your table name>'

How to get the primary column name in SQL Server?

Here are a few lines of sql query using which we can get the primary column name. Here we join these two views, TABLE_CONSTRAINTS And CONSTRAINT_COLUMN_USAGE on CONSTRAINT_NAME. Then select those records where CONSTRAINT_COLUMN_USAGE.TABLE_NAME is Employee and TABLE_CONSTRAINTS.CONSTRAINT_TYPE is Primary Key.

How can I know the next value in SQL Server?

With the current version of SQL Server (2008 R2), and using the IDENTITY mechanism, you CANNOT know the next value ahead of time. There is no proper, guaranteed way to know the next value until you've actually inserted the row - only then, when the row is stored inside the table, that value is determined and returned.

How do I filter a table without a primary key?

This view returns a row for each user table, and when we use OBJECTPROPERTY () to filter the results based on the TableHasPrimaryKey property being 0, we get just those tables without a primary key. In this case, my current database is a test database with a bunch of tables without primary keys.


2 Answers

EDIT (Very important)

It should be noted that this method can be used to predict the next id, but does not gaurentee this value. The reason for this is as @marc_s mentioned in the comments, that between the time that you requested the value, and the time you use it, another transaction could have inserted into this table, making the assumption of the value retrieved null and void.

As mentioned, if your implementation is based on such an assumption, you have made some design errors, and should look at reworking this solution as a first priority.

From IDENT_CURRENT (Transact-SQL)

Returns the last identity value generated for a specified table or view. The last identity value generated can be for any session and any scope.

Have a look at the following example

CREATE TABLE #Table (
        ID INT IDENTITY(1,1),
        Val INT
)

INSERT INTO #Table SELECT 1
INSERT INTO #Table SELECT 2
INSERT INTO #Table SELECT 3

SELECT * FROM #Table

DELETE FROM #Table WHERE ID >= 2

SELECT * FROM #Table

SELECT IDENT_CURRENT('#Table')

DROP TABLE #Table
like image 164
Adriaan Stander Avatar answered Sep 20 '22 02:09

Adriaan Stander


What you're asking doesn't really make sense. Databases are designed to support multiple access so even if there was a way to determine the next primary key identity without inserting a record there would be zero guarantee that another connection wouldn't write to that table and claim that identity before your next transaction completes. And if you're thinking "Well I could keep a lock on it" then you've basically eliminated any sort of plausible situation where knowing the future identity key might help you.

So what is your reasoning for wanting the future identity key?

like image 32
Spencer Ruport Avatar answered Sep 19 '22 02:09

Spencer Ruport