我在sql server 2008中有两个表A和B. 两列中都存在Id列 表A有一个新列Result 问题是表B中ID列的所有值都与表A中的id列的值匹配,那么我应该在表A的结果列中输入一个TRUE,否则为false
提前致谢
得到答案!!如果表有1000万行,则不确定性能 试过自己并得到了答案
声明临时表
声明@temp表 (id int,result varchar)
插入@temp选择A1.id,来自A A1内部连接B B1的A1.result A1.id = B1.id
更新@temp set result = true
答案 0 :(得分:0)
您必须按以下方式运行2个查询才能实现您的要求。
update A set A.result = true from tableA A, tableB B where A.Id = B.Id
update A set A.result = false from tableA A, tableB B where A.Id != B.Id
答案 1 :(得分:0)
declare @A as Table ( Id Int, Result Bit NULL );
declare @B as Table ( Id Int );
insert into @A ( Id ) values ( 1 ), ( 2 ), ( 4 ), ( 8 );
insert into @B ( Id ) values ( 1 ), ( 2 ), ( 3 ), ( 4 );
select * from @A;
select * from @B;
update A
set Result = case when B.Id is NULL then 0 else 1 end
from @A as A left outer join
@B as B on B.Id = A.Id;
select * from @A;
答案 2 :(得分:0)
试试这个
Update tableA a
set result = true
where exists (select 1 from tableB b where a.id=b.id);
Update tableA a
set result = false
where not exists (select 1 from tableB b where a.id=b.id);