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!
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.
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.
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.
DECLARE @uid uniqueidentifier
SET @uid = newid()
INSERT INTO sample (ID, Title)
VALUES (@uid,'Test1')
SELECT @uid AS ID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With