Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 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

3873 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
52326 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  
 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.03 seconds. Powered By: Snitz Forums 2000