Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last inserted UNIQUEIDENTIFIER in SQL Server 2000

The OUTPUT clause is compatible with SQL Server 2005 but not SQL Server 2000.

How do I convert this command to work in SQL Server 2000?

CREATE TABLE sample
(
 ID uniqueidentifier NOT NULL DEFAULT newid(),
 Title varchar(30) NOT NULL
)

INSERT INTO sample (Title)
OUTPUT INSERTED.ID
VALUES ('Test1')

I need the command to retrieve the ID since the INSERT command needs to be called from a stored procedure.

Thanks for any help!

like image 389
Shay Avatar asked May 02 '11 22:05

Shay


People also ask

How do I get the last inserted record ID in SQL Server?

IDENT_CURRENT() will give you the last identity value inserted into a specific table from any scope, by any user. @@IDENTITY gives you the last identity value generated by the most recent INSERT statement for the current connection, regardless of table or scope.

How do you get the last inserted identity value?

Use @@IDENTITY to Return the Last-Inserted Identity Value in SQL Server. In SQL Server, you can use the T-SQL @@IDENTITY system function to return the last-inserted identity value in the current session. Note that it returns the last identity value generated in any table in the current session.

How do I get Uniqueidentifier in SQL Server?

SQL Server NEWID to Generate GUID Let's create a variable of uniqueidentifier data type. Type the below code in SSMS and execute. DECLARE @guid uniqueidentifier = NEWID(); SELECT @guid as 'GUID'; Here we created a variable named guid of data type uniqueidentifier.


1 Answers

DECLARE @uid uniqueidentifier 
SET @uid  = newid()

INSERT INTO sample (ID, Title)
VALUES (@uid,'Test1')

SELECT @uid AS ID
like image 152
Martin Smith Avatar answered Sep 21 '22 10:09

Martin Smith