How to get result from Update statement ?

ahmed salah 3,216 Reputation points
2020-10-26T21:15:08.753+00:00

How to get result of Update statement in output statement

I work on sql server 2012

update r set r.PartsLCUpdated=1,r.PartsLCDate=getdate(), r.IsValid=g.IsValid, r.HasReplacementCode=g.NewHasReplacementCode, r.JobUpdateHasreplacement=g.JobUpdateHasreplacement
output Inserted.PartsLCUpdated,Inserted.PartsLCDate,Inserted.IsValid,Inserted.HasReplacementCode,Inserted.JobUpdateHasreplacement into #getfinal
from replaceTest2 r with(nolock)
inner join #GetDataElastic g on g.PartIDC=r.partidc and g.PartIDX=r.partidx and g.OldHasReplacementCode=r.HasReplacementCode and g.IsValid=r.IsValid

I need to get result of update statement on output
so How to do that please ?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,677 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,625 questions
0 comments No comments
{count} votes

Accepted answer
  1. MelissaMa-MSFT 24,196 Reputation points
    2020-10-27T01:48:20.96+00:00

    Hi @ahmed salah ,

    You could declare a table variable called @getfinal to store the results of the OUTPUT clause into this table variable. Then you could use the OUTPUT INTO syntax in order to store the results into a table variable.

    Please refer below:

    DECLARE @getfinal table( PartsLCUpdated int,  
                            PartsLCDate datetime,  
                            IsValid varchar(50),  
                            HasReplacementCode varchar(50),  
                            JobUpdateHasreplacement varchar(50));  
      
    update r   
    set r.PartsLCUpdated=1,r.PartsLCDate=getdate(),r.IsValid=g.IsValid, r.HasReplacementCode=g.NewHasReplacementCode, r.JobUpdateHasreplacement=g.JobUpdateHasreplacement  
    output   
    Inserted.PartsLCUpdated,Inserted.PartsLCDate,Inserted.IsValid,Inserted.HasReplacementCode,Inserted.JobUpdateHasreplacement   
    into @getfinal  
    from replaceTest2 r with(nolock)  
    inner join #GetDataElastic g on g.PartIDC=r.partidc and g.PartIDX=r.partidx and g.OldHasReplacementCode=r.HasReplacementCode and g.IsValid=r.IsValid  
      
    Select * from @getfinal  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2020-10-26T21:39:21.29+00:00
    DECLARE @tbl TABLE (
        Id int
    );
    INSERT INTO @tbl VALUES (1), (10);
    
    UPDATE @tbl 
    SET Id = Id * 2
    OUTPUT inserted.Id;
    

    You can insert the output into a table variable.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.