-1

我在 sql server 2008 中有两个表 A 和 B 。两个表中都存在 Id 列表 A 有一个新列结果问题是表 B 中 ID 列的所有值与表 A 中 id 列的值匹配,那么我应该在结果中放置一个 TRUE 否则为 false表A的列

提前致谢

得到答案!!如果表有 1000 万行,不确定性能我自己试过并得到了答案

声明一个临时表

声明 @temp 表(id​​ int,结果 varchar)

插入@temp select A1.id,A1.result from A A1 inner join B B1 on A1.id =B1.id

更新 @temp 设置结果 = true

4

3 回答 3

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
于 2013-06-15T19:42:21.790 回答
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;
于 2013-06-15T20:09:40.070 回答
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);
于 2013-06-15T20:10:32.863 回答