我正在尝试从另一个表中搜索值,如果6-7列中的任何一列具有不同的值,那么我将把它们插入到现有的表中并更改标志。
有没有一种特定的方法可以更快地搜索,或者我应该使用顺序搜索?不确定这在SQL中是如何工作的
例如:
表A
A B C D Flg
1 2 3 4 N
2 1 4 3 N
3 3 2 1 N
4 4 1 2 N
5 2 1 2 N
1 3 3 4 Y --(flg changed because values in column B changed)
发布于 2016-07-14 01:40:39
我不确定我是否跟踪了数据来自哪里,或者是哪个表在接收数据。如果您一次只处理一行,并且它需要在一个自包含的insert
中,那么这可能就是您所需要的。
;with MyData(A, B, C, D) as (
select * from (values (1, 3, 3, 4)) as v
)
insert into Table1 (A, B, C, D, Flg)
select A, B, C, D, 'Y'
from MyData as d
where not exists (
select 1 from Table2 as t
where t.A = d.A and t.B = d.B and t.C = d.C and t.D = d.D
);
我有一种预感,可能有一种比你已经准备好的任何方法更干净的方法来做这件事。
https://stackoverflow.com/questions/38358177
复制相似问题