私はSOで検索しましたが、私の目的には何も見つかりませんでした。あるテーブルから別のテーブルにのみ一意の行を挿入する必要があります。私は:SQL Serverは複数の列にのみ基づいて新しい行を挿入します
TABLE1
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1331 elm 04/01/2016 04/30/2016 05/01/2016 5656
表2
id name bookid bookname start_date end_date rel_date rel_id
1 horror 1221 rockys 04/01/2016 04/30/2016 05/01/2016 4545
2 horror 1441 elm 04/01/2016 04/30/2016 05/01/2016 5656
をIは表2にID = 2と行TABLE1に挿入する必要があり、また、ID = 2と行を削除します残りの列が一致してもbookidが異なるため、table1から取得します。
insert into table1
select * from table2
where not exists (select * from table2 where table1.id = table2.id
and table1.name = table2.name and table1.bookid = table2.bookid and
table1.bookname = table2.bookname and table1.start_date = table2.start_date
and table1.end_date = table2.end_date and table1.rel_date = table2.rel_date
and table1.rel_id = table2.rel_id)
私は1つのSQLブロック内にこのすべてを行うことができます任意の方法: 私は、次の試してみましたか?
最初に削除して次に挿入するために結合を使用する – techspider
ちょうどbookidに問題がある場合、なぜ結合で更新クエリを書くことができないのですか? – techspider
また、TSQLの 'MERGE'コマンドを調べることもできます。 –