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


OUTPUT clause with INSERT


OUTPUT clause with INSERT

Author
Message
lanky_doodle
lanky_doodle
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1913 Visits: 178
Hello,

Is there anyway I can replicate this within an INSERT/OUTPUT statement:

declare @ID as table(ID uniqueidentifier);

insert into tbl_Link_Update_History

output inserted.ID into @ID

values (newid(), @LinkID, getdate(), @UserID);

select dbo.fnc_FormatDate(luh.Updated_Date, 106) + ' (' + u.Forename + ' ' + u.Surname + ')'

from tbl_Link_Update_History luh join

tbl_Users u on u.[User_ID] = luh.Updated_By_ID

where luh.ID =

(

select ID

from @ID

);



The needed result is a scalar value: 1 January 1900 (Firstname Lastname)

Thanks

Thom A
Thom A
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96448 Visits: 23423
I don't understand you're question. What are you trying to do here exactly? Could you please elaborate.

Thanks.


Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does :-P

Please always remember to encapsulate your code in IFCode Markup. For example [code=sql] [/code].
Click here to read Jeffs Guide on how to post SQL questions, and get swift and helpful answers from the community
sgmunson
sgmunson
SSC Guru
SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)

Group: General Forum Members
Points: 108775 Visits: 7524
lanky_doodle - Thursday, May 3, 2018 5:39 AM
Hello,

Is there anyway I can replicate this within an INSERT/OUTPUT statement:

declare @ID as table(ID uniqueidentifier);

insert into tbl_Link_Update_History

output inserted.ID into @ID

values (newid(), @LinkID, getdate(), @UserID);

select dbo.fnc_FormatDate(luh.Updated_Date, 106) + ' (' + u.Forename + ' ' + u.Surname + ')'

from tbl_Link_Update_History luh join

tbl_Users u on u.[User_ID] = luh.Updated_By_ID

where luh.ID =

(

select ID

from @ID

);



The needed result is a scalar value: 1 January 1900 (Firstname Lastname)

Thanks

Part of the problem is that you are asking for a SCALAR result from a SELECT from a table. If you ever have more than one row in that table, it's not going to work. If you were to just change the = character to the word IN, you'd have a functioning query, but we don't know for sure if that's what you actually need.


Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
sgmunson
sgmunson
SSC Guru
SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)

Group: General Forum Members
Points: 108775 Visits: 7524
lanky_doodle - Thursday, May 3, 2018 5:39 AM
Hello,

Is there anyway I can replicate this within an INSERT/OUTPUT statement:

declare @ID as table(ID uniqueidentifier);

insert into tbl_Link_Update_History

output inserted.ID into @ID

values (newid(), @LinkID, getdate(), @UserID);

select dbo.fnc_FormatDate(luh.Updated_Date, 106) + ' (' + u.Forename + ' ' + u.Surname + ')'

from tbl_Link_Update_History luh join

tbl_Users u on u.[User_ID] = luh.Updated_By_ID

where luh.ID =

(

select ID

from @ID

);



The needed result is a scalar value: 1 January 1900 (Firstname Lastname)

Thanks

And now that I look a little closer, you have a different kind of problem. You are expecting to limit a query result to values in a table that is only getting inserted into by the output of that very query. It's NEVER going to work. I'm not even sure what you think the result of this will be....

EDIT: I have to retract this... I missed the values clause and that matters. Just change the = to IN and you should be good to go, assuming that the other tables do have data in them that match the criteria specified.


Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)SSC Guru (438K reputation)

Group: General Forum Members
Points: 438333 Visits: 44193
sgmunson - Tuesday, May 8, 2018 7:00 AM
lanky_doodle - Thursday, May 3, 2018 5:39 AM
Hello,

Is there anyway I can replicate this within an INSERT/OUTPUT statement:

declare @ID as table(ID uniqueidentifier);

insert into tbl_Link_Update_History

output inserted.ID into @ID

values (newid(), @LinkID, getdate(), @UserID);

select dbo.fnc_FormatDate(luh.Updated_Date, 106) + ' (' + u.Forename + ' ' + u.Surname + ')'

from tbl_Link_Update_History luh join

tbl_Users u on u.[User_ID] = luh.Updated_By_ID

where luh.ID =

(

select ID

from @ID

);



The needed result is a scalar value: 1 January 1900 (Firstname Lastname)

Thanks

And now that I look a little closer, you have a different kind of problem. You are expecting to limit a query result to values in a table that is only getting inserted into by the output of that very query. It's NEVER going to work. I'm not even sure what you think the result of this will be....


The only problem I see with the second query is the use of the '=' sign instead of using IN.

But then again, I could be wrong.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sgmunson
sgmunson
SSC Guru
SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)SSC Guru (108K reputation)

Group: General Forum Members
Points: 108775 Visits: 7524
Lynn Pettis - Tuesday, May 8, 2018 9:58 AM
sgmunson - Tuesday, May 8, 2018 7:00 AM
lanky_doodle - Thursday, May 3, 2018 5:39 AM
Hello,

Is there anyway I can replicate this within an INSERT/OUTPUT statement:

declare @ID as table(ID uniqueidentifier);

insert into tbl_Link_Update_History

output inserted.ID into @ID

values (newid(), @LinkID, getdate(), @UserID);

select dbo.fnc_FormatDate(luh.Updated_Date, 106) + ' (' + u.Forename + ' ' + u.Surname + ')'

from tbl_Link_Update_History luh join

tbl_Users u on u.[User_ID] = luh.Updated_By_ID

where luh.ID =

(

select ID

from @ID

);



The needed result is a scalar value: 1 January 1900 (Firstname Lastname)

Thanks

And now that I look a little closer, you have a different kind of problem. You are expecting to limit a query result to values in a table that is only getting inserted into by the output of that very query. It's NEVER going to work. I'm not even sure what you think the result of this will be....


The only problem I see with the second query is the use of the '=' sign instead of using IN.

But then again, I could be wrong.

It will operate once you change = to IN. The @ID table is defined, but I'm doubtful any rows would get selected. That @ID table starts with no rows, and is the subject of inserts only from the results of that same query, which should provide no rows because it goes after rows that match a value in an empty table. Thus there's no way for data to make it into that table. If SQL didn't see the 0 rows there and shortcut the query execution to return 0 rows in the result set, I'd be very surprised.

I just realized that I missed the values clause entirely. I'm retracting my post... my bad...



Steve
‌(aka sgmunson)
Smile Smile Smile
Health & Nutrition
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