select last insert ID
-
Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene
-
Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene
with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh
-
with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh
-
Hallo; I would Like to know how to select last insert id from one table so as i can insert it to another table on the same event holder using SQL SERVER 2000 /2005 I am able to do the same using Mysql using the last insert Id function but cannot do the same for SQL server. briogene
Select scope_identity() -- modified at 5:31 Monday 22nd October, 2007
Best Regards, Chetan Patel
-
Your Welcome... Regards Avesh
-
with the help of @@identity and u can use this query as well select max(id) from tablename After the insertion of values I hope this will work for u Regards Avesh
You really shouldn't do either of these, as they are both pretty bad practice. If you do a select on @@identity, you get the last inserted identity column regardless of scope. So, suppose you insert a record into your table and there is a trigger behind there that inserts into another table with an identity column on it, @@identity returns the id of the trigger insert and not the main table. SELECT max(id) only works if there aren't multiple people posting into your tables at the same time. The simple solution is to use:
SELECT @ID = SCOPE_IDENTITY()
This returns the identity of the inserted item on the main table, even if you have lots and lots of trigger inserts going on.
Deja View - the feeling that you've seen this post before.