Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get identifier back after performing INSERT

Author  Topic 

vermorel
Starting Member

26 Posts

Posted - 2006-02-08 : 15:55:38
Basically I have a table with 2 fields UserId (string) and UserName (uniqueidentifer). The default value for UserId is newid().

I can perform an insertion with
INSERT INTO MyTable(UserName) VALUES ('Foo Bar');

But I would like to retrieve the UserId that has just been created. How do I achieve that?

Thanks in advance,
Joannes

nr
SQLTeam MVY

12543 Posts

Posted - 2006-02-08 : 16:07:31
declare @UserId uniqueidentifer
select @UserId = newid()
INSERT INTO MyTable(UserId , UserName) select UserId , 'Foo Bar'
select @UserId


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2006-02-08 : 21:35:47
RETURN SCOPE_IDENTITY()


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-02-09 : 01:03:36
quote:
Originally posted by jhermiz

RETURN SCOPE_IDENTITY()


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]

RS Blog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]


Thats for Identity column and not for uniqueidentifer column
Nigel is correct so as this
http://vyaskn.tripod.com/retrieve_guid_value_like_identity.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -