Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Where to put OUTPUT
Message
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
01673317
Message ID:
01673323
Views:
39
>>>>Hi,
>>>>
>>>>Where do you put an OUTPUT command in a SQL Update of the following "structure"?
>>>>
>>>>
>>>>update t1 set @Qty2Use = case when t1.qty_left >= @QtyUseRemain then @QtyUseRemain else t1.qty_left end,  
>>>>t1.qty_left = t1.qty_left - @Qty2Use
>>>>from #InvStockTemp t1 inner join (select top (1) ......
>>>>.....
>>>>
>>>>
>>>>I declared the temp variable as follows:
>>>>
>>>>DECLARE @UpdateOutput table
>>>>(
>>>>  QtyLeftOld int,
>>>>  QtyLeftNew int
>>>>);
>>>>
>>>>
>>>>But when I put the OUTPUT right before FROM #InvStockTemp, I get an error.
>>>>
>>>>Where do I put it?
>>>>
>>>>TIA
>>>
>>>See sample E in this https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-ver15
>>
>>Already have a question about OUTPUT.
>>
>>In the UPDATE I set the new value of the column QTY_LEFT. And then, based on the value in this column, I have to set the value of the column REC_ID. So, my understanding - since the order of evaluation is not certain - to use the New Value of the column QTY_LEFT when setting the value of REC_ID.
>>So, my question, can I SET the value of column REC_ID after I have the OUTPUT command?
>
>It will be a new separate UPDATE command.

Then, if I understand this correctly, I have to do the Select Top(1) twice. That is, the "long" UPDATE command finds the oldest (chronologically) record in the table. Then it update the column QTY_LEFT.
Then, I have to find this record again, in order to use the OUTPUT variable and update the column REC_ID.
Do I understand the sequence correctly?
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform