Solved

How to get lastest incremented UNIQUEIDENTIFIER?

Posted on 2004-08-24
4
214 Views
Last Modified: 2012-05-05
Hello Everyone!
       As last inserted value can be got through "Select @@Identifier" if we are using INTEGER datatype but what if we use UNIQUEIDENTIFIER datatype, how do we get last inserted value?

                                                                       Thanx,

                                                                       Adnan
0
Comment
Question by:kamran_khan
4 Comments
 
LVL 11

Accepted Solution

by:
ram2098 earned 500 total points
ID: 11878604
There isn't a way to get the value of a uniqueidentifier column after an insert. You can get it before the insert, however, by using the below code.

DECLARE @GUID uniqueidentifier
SET @GUID = NEWID()
INSERT Item VALUES (@GUID,'Yak Hoof')
0
 
LVL 4

Expert Comment

by:davehilditch
ID: 11880320
And additonally, you shouldn't really use @@IDENTITY in the first place, as if you have a trigger on the table which then inserts to another table you will receive the wrong value.  For identity columns, you should use scope_identity() instead.

Dave Hilditch.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now