This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get the primary key of the last row inserted into the table
In SQL Server 2008, I have a stored proc that inserts in a table which includes identity column for ID. I need to return the ID of record to my application so that I can use it for related tables.
How can I get generated ID by SQL?
Use select Scope_Identity() to get the ID
A separate SQL statement
SELECT SCOPE_IDENTITY()
Or the OUTPUT clause
INSERT MyTable (...=
OUTPUT INSERTED.KeyCol
VALUES (...) --Or SELECT ... FROM Another table)
There are multiple options that are a bit different:
SCOPE_IDENTITY() - that's what I would use
IDENT_CURRENT( 'table_name' )
##IDENTITY
Related
This question already has answers here:
Sql Server return the value of identity column after insert statement
(7 answers)
How to get the identity of an inserted row?
(15 answers)
Closed 28 days ago.
I have a result set like below, RefId column will be empty initially. I need to loop through this result set insert values into one table ex : TableA , get the inserted row's identity (primary key) column value of the inserted rows and do some more manipulations.
I tried with cursors, since they are not recommended to use, how else can I do this in tsql ?
Result set query :
Insert into TableA(column1,column2,column3)
select column1, column2, column3 from Table B -- I want the identity values of the rows inserted into TableA,
I need them in another result set/ temp table or table variable along with the source values (TableB's select list values)
This question already has answers here:
How to insert a record with only default values?
(3 answers)
Closed 5 years ago.
SQL Server 2014
Given a table 'test' with the following fields:
id: int. Primary key. Autoincrement by 1.
creation_date: datetime. GETDATE() by default value.
My intention is to insert an empty statement, just to register the event of the insertion. I thought that as the id is an autoincrement and the creation_date has a value by default, a record could be inserted without specifying any value. I know that this can be done by adding a third field and specifying the value in the insertion, but my question is:
Can something like a plain INSERT INTO test be done?
Thanks
INSERT INTO test DEFAULT VALUES
This question already has answers here:
Return the id of the just added row
(5 answers)
Closed 8 years ago.
I am using SQL Server Management Studio. I have table named faculty and its attribute are id, Name and dean. I want to get last inserted record id in this table.
eg:
id Name dean
1 abc xyz
2 efg sss
3 ert yui
I just want to get 3rd id only, not 3rd row.
Just last inserted id that is 3 in this case.
You can use the SCOPE_IDENTITY() in your stored procedure to get last inserted record id.
SELECT SCOPE_IDENTITY() :- It will returns the last identity value inserted into an
identity column in the same scope.
SELECT ##IDENTITY :- ##IDENTITY will return the last identity value entered
into a table in your current session.
SELECT IDENT_CURRENT(‘tablename’) :- IDENT_CURRENT is not limited by scope and
session; it is limited to a specified table.
There are other options also such as
1. Select Top 1 id From Table Order by id desc
2. SELECT max(id) FROM table
Refer msdn http://msdn.microsoft.com/en-us/library/ms190315.aspx
and
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
You can Use
##IDENTITY, SCOPE_IDENTITY, IDENT_CURRENT
see this Link ##IDENTITY, SCOPE_IDENTITY, IDENT_CURRENT
This question already has answers here:
How to get last inserted id?
(16 answers)
Closed 10 years ago.
I want to get the new created ID when you insert a new record in table.
I read this: http://msdn.microsoft.com/en-us/library/ms177564.aspx but it needs to create temporary table.
I want to return the ID after executing INSERT statement (assuming executing just one INSERT).
Example:
1 Joe Joe
2 Michael Mike
3 Zoe Zoe
When executing an INSERT statement, I want to return the created ID, means 4.
Can tell me how to do that using SQL statement or it is not possible ?
If your SQL Server table has a column of type INT IDENTITY (or BIGINT IDENTITY), then you can get the latest inserted value using:
INSERT INTO dbo.YourTable(columns....)
VALUES(..........)
SELECT SCOPE_IDENTITY()
This works as long as you haven't inserted another row - it just returns the last IDENTITY value handed out in this scope here.
There are at least two more options - ##IDENTITY and IDENT_CURRENT - read more about how they works and in what way they're different (and might give you unexpected results) in this excellent blog post by Pinal Dave here.
Assuming a simple table:
CREATE TABLE dbo.foo(ID INT IDENTITY(1,1), name SYSNAME);
We can capture IDENTITY values in a table variable for further consumption.
DECLARE #IDs TABLE(ID INT);
-- minor change to INSERT statement; add an OUTPUT clause:
INSERT dbo.foo(name)
OUTPUT inserted.ID INTO #IDs(ID)
SELECT N'Fred'
UNION ALL
SELECT N'Bob';
SELECT ID FROM #IDs;
The nice thing about this method is (a) it handles multi-row inserts (SCOPE_IDENTITY() only returns the last value) and (b) it avoids this parallelism bug, which can lead to wrong results, but so far is only fixed in SQL Server 2008 R2 SP1 CU5.
You can use:
SELECT IDENT_CURRENT('tablename')
to access the latest identity for a perticular table.
e.g. Considering following code:
INSERT INTO dbo.MyTable(columns....) VALUES(..........)
INSERT INTO dbo.YourTable(columns....) VALUES(..........)
SELECT IDENT_CURRENT('MyTable')
SELECT IDENT_CURRENT('YourTable')
This would yield to correct value for corresponding tables.
It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.
I ask about alternative or similar query in informix to perform the following:
INSERT INTO days (depcode,studycode,batchnum) values (1,2,3);SELECT SCOPE_IDENTITY();
I want a query to return the SCOPE_IDENTITY() during insertion statement
I know that in t-sql you have the OUTPUT statement?
Where [KEY] is the column name of your primary key and #OUT_KEY is a variable you need to declare
INSERT INTO days
(
depcode,
studycode,
batchnum
)
OUTPUT INSERTED.[KEY] INTO #OUT_KEY
VALUES
(
1,2,3
)
EDIT
For informix you can use
SELECT DBINFO( 'sqlca.sqlerrd1' )
FROM systables
WHERE tabid = 1;
Presuming your pk column is SERIAL
I use this sql statement Select ##Identity after I inserted rows. It gives me the ID of the last inserted row. I´m using an accessdatabase. I don´t know if it work with your database.