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
 Getting Identity Field

Author  Topic 

User Name
Starting Member

3 Posts

Posted - 2011-04-21 : 18:51:51
I am doing a web application and I am displaying the Userid as a confirmation number when the user saves his information. Right now, I am getting the Max value of the column(UserID-identity field).Now, if two users are registering at same time, would it be problem ? Since I am gettng the Max Value , Is there any chance of getting a wrong ID? Is it better to use the Scope Identity for getting the Identity field?

Thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-21 : 18:54:37
You should definitely be using SCOPE_IDENTITY().

Never use MAX to get the identity value of the just inserted row.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

User Name
Starting Member

3 Posts

Posted - 2011-04-25 : 14:27:50
quote:
Originally posted by tkizer

You should definitely be using SCOPE_IDENTITY().

Never use MAX to get the identity value of the just inserted row.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



How do i use the Scope_Identity()


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-25 : 14:29:13
Did you look in Books Online or google it? This is a very trivial thing and can very quickly be found by a quick search.

Here is a very quick example though:

DECLARE @i int

INSERT INTO tbl1 (...) VALUES (...)

SET @i = SCOPE_IDENTITY()

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

User Name
Starting Member

3 Posts

Posted - 2011-04-25 : 14:34:49
quote:
Originally posted by tkizer

Did you look in Books Online or google it? This is a very trivial thing and can very quickly be found by a quick search.

Here is a very quick example though:

DECLARE @i int

INSERT INTO tbl1 (...) VALUES (...)

SET @i = SCOPE_IDENTITY()

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Thank you..
Go to Top of Page
   

- Advertisement -