Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent for Merge with output clause in SQL Server to Postgresql

I want to insert values based on below code to a temporary table in postgresql

declare @output table (AuditScratchID bigint, AuditID bigint);

merge table atb
 using (select 
            s.ID
            ....
            ....
            ....
        from @temporaryTableVariable s
            inner join ....
...............
..............

        ) as s
 on 1 = 2 -- Impossible Condition so they never match
 when not matched then
    insert (.....)
    values (.....)
    output s.ID, inserted.ID
    into @output;

Just to mention, how can I correlate values into temporary table

like image 494
swapnil solanki Avatar asked Oct 18 '25 19:10

swapnil solanki


1 Answers

I don't understand the use of MERGE to begin with.

This seems like a straightforward insert ... select. To see the inserted rows, use the returning clause

insert into atb (...)
select ... columns ...
from some_table
  join other_table on ...
returning *