Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is Scope_Identity() only valid if table has int key value. If table has string key how can I use this to take last updated/added key.
Posted

Scope_Identity only works on identity fields in a database. I think you are being confused with Primary Fields and Identity Fields. An Identity field is only updated by the pertaining RDMS in their own particular fashion(most implement AutoNumber fields, some implement GUID fields), while Primary keys can be any datatype, and are usually updated by the developer/stored proc etc. Whilst a Primary field can also be an Identity field, it is not necessarily so. Scope_Identity will therefore only work on Identity columns.
 
Share this answer
 
Comments
Sanyon_d 22-Mar-11 5:42am    
Thanks very much for your reply.
hi,
try this
INSERT INTO mySchoolRecord (StudentId,Name,[Address],Contact) OUTPUT INSERTED.StudentId VALUES ('abcdesfg','Mr. Peter','United States', 256 )

if your are using Guid then try this

DECLARE @ClientID uniqueidentifier
DECLARE @MyTableVar table ( ClientID uniqueidentifier )

INSERT INTO Client (ClientName, Address1, Address2)
OUTPUT INSERTED.ClientID INTO @MyTableVar
VALUES ('AAAA', 'Street', 'place')
SELECT @ClientID = ClientID FROM @MyTableVar
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900