SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Store Procedure Output Parameter INSERTED.ID
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

swissivan
Starting Member

30 Posts

Posted - 03/03/2013 :  20:48:43  Show Profile  Reply with Quote
I want to write a store procedure that return the inserted id to my c# program

@id uniqueidentifier OUTPUT

INSERT INTO product(name,...,etc)
OUTPUT INSERTED.id, INSERTED.mycolumn
VALUES ('myname',...,etcvalue)

I want to pass the OUTPUT INSERTED.id to @id
How can do this? thanks

INSERTED.id is in type uniqueidentifier

Edited by - swissivan on 03/03/2013 21:01:09

James K
Flowing Fount of Yak Knowledge

3652 Posts

Posted - 03/03/2013 :  21:00:46  Show Profile  Reply with Quote

@id uniqueidentifier OUTPUT
DECLARE @idTbl TABLE (id UNIQUEIDENTIFIER);

INSERT INTO product(name,...,etc)
OUTPUT INSERTED.id INTO @idTbl
OUTPUT INSERTED.id, INSERTED.mycolumn
VALUES ('myname',...,etcvalue)

SELECT TOP (1) @id = id FROM @idTbl;
Of course, if you have more to the values clause (i.e., you are inserting more than one row via the insert statement), you will get only one of the values in the @id variable.
Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 03/03/2013 :  21:22:55  Show Profile  Reply with Quote
quote:
Originally posted by James K


@id uniqueidentifier OUTPUT
DECLARE @idTbl TABLE (id UNIQUEIDENTIFIER);

INSERT INTO product(name,...,etc)
OUTPUT INSERTED.id INTO @idTbl
OUTPUT INSERTED.id, INSERTED.mycolumn
VALUES ('myname',...,etcvalue)

SELECT TOP (1) @id = id FROM @idTbl;
Of course, if you have more to the values clause (i.e., you are inserting more than one row via the insert statement), you will get only one of the values in the @id variable.



It works great, thank you very much!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/03/2013 :  22:30:37  Show Profile  Reply with Quote
quote:
Originally posted by James K


@id uniqueidentifier OUTPUT
DECLARE @idTbl TABLE (id UNIQUEIDENTIFIER);

INSERT INTO product(name,...,etc)
OUTPUT INSERTED.id INTO @idTbl
OUTPUT INSERTED.id, INSERTED.mycolumn
VALUES ('myname',...,etcvalue)

SELECT TOP (1) @id = id FROM @idTbl;
Of course, if you have more to the values clause (i.e., you are inserting more than one row via the insert statement), you will get only one of the values in the @id variable.


just using top 1 wont guarantee the order of retrieval

you might need to use ORDER BY id DESC

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000