How to merge insert a column from a temp table and parameters from stored procedure?

2k Views Asked by At

is this the "right" way to do that?

merge dbo.tableA as tgt
using (select #temptable.pkid, @spParam1 as col1, @spParam2 as col2 from #temptable)
as src
on tgt.pkid = src.pkid
when not matched by target when
   insert (pkid, thing1, thing2) values (src.pkid, col1, col2)
;

Is there a different or better way?

1

There are 1 best solutions below

0
On

without using merge this sql will work for you.

insert into dbo.tableA (pkid, thing1, thing2)
   select #temptable.pkid, @spParam1 as col1, @spParam2 as col2 from #temptable src
   left join dbo.tableA as tgt on tgt.pkid = src.pkid