嗨存在我有这样一段代码:MSSQL - 插入一行到一个表,如果该行不已经在表中
insert into table1(column1, column2)
select distinct id, somevalue from table1 where id in (select id from table2)
基本上是将所有的ID从表2至表1列1,而table1中的其他列为所有行共享相同的值。
现在的问题是,我必须检查table2中的id是否已经存在于table1中,并且只有在不存在时才会插入。我怎样才能做到这一点?
正如你所写的查询'insert into。 。 。选择ID。 。 。来自table1。 。 。*,*所有*的ID都将存在于表1中。你能编辑这个问题吗? –
您可能要使用'MERGE':http://msdn.microsoft.com/en-us/library/bb510625.aspx – aidan