Possible Duplicate:
SQL Server Output Clause into a scalar variable
DECLARE @id int INSERT INTO MyTable(name) OUTPUT @id = Inserted.id VALUES('XYZ')
I am trying like above. How is it possible?
When a variable is first declared, its value is set to NULL. To assign a value to a variable, use the SET statement. This is the preferred method of assigning a value to a variable. A variable can also have a value assigned by being referenced in the select list of a SELECT statement.
Use table variable to get id
DECLARE @id int DECLARE @table table (id int) INSERT INTO MyTable(name) OUTPUT inserted.id into @table VALUES('XYZ') SELECT @id = id from @table
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