如何合并插入临时表中的列和存储过程中的参数?

时间:2013-12-16 15:39:04

标签: sql sql-server tsql sql-insert sql-merge

这是“正确”的方式吗?

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)
;

有不同或更好的方法吗?

1 个答案:

答案 0 :(得分:1)

不使用合并,这个SQL将适合你。

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