SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


OUTPUT clause


OUTPUT clause

Author
Message
Ray K
Ray K
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3851 Visits: 4646
Hi folks . . . hopefully this is just a quick question.

Was just reading BOL regarding the OUTPUT clause. I'd like to take a column value generated from an INSERT and pass it to a variable.

Does the variable have to be a TABLE variable, or is there a way to pass it into a common variable such as INT or NVARCHAR (or, in my particular situation, a UNIQUEIDENTIFIER)?

Thanks!

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10178 Visits: 10575
You can either use a table (table variable, temporary table, permanent table) (results to into it), or not (results to client). But not to a "regular" variable. See the BOL OUTPUT Clause entry.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Ray K
Ray K
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3851 Visits: 4646
Okay, that's what I figured.

I ended up just creating a simple SELECT statement for the column immediately following the INSERT. I'm just writing a quick 'n dirty, and it's insignificant enough that having to create a TABLE variable for it would be unnecessary overkill.

Thanks!

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10178 Visits: 10575
Ray K (10/21/2010)
Okay, that's what I figured.

I ended up just creating a simple SELECT statement for the column immediately following the INSERT. I'm just writing a quick 'n dirty, and it's insignificant enough that having to create a TABLE variable for it would be unnecessary overkill.

Thanks!


Say what? Overkill???

DECLARE @test TABLE (Col1 uniqueidentifier);

INSERT INTO .....
OUTPUT i.MyUniqueIdentifierColumn INTO @test
....

SELECT * FROM @test;



Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Ray K
Ray K
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3851 Visits: 4646
WayneS (10/21/2010)
Say what? Overkill???

Edit: my original reply was a bit too flippant -- didn't want anyone to take it the wrong way!

Maybe "overkill" wasn't the right word -- rather, what I was doing wasn't terribly big, and I didn't think it necessitated the need to create a table variable.

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
Muhammad Tariq
Muhammad Tariq
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 245
see Examples at below link:

http://mssqlsolutions.blogspot.com/2008/08/how-to-use-output-clause.html


Tariq
master your setup, master yourself.
http://mssqlsolutions.blogspot.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search