0

例如,Table1 定义为 (id int, address varchar(100))

将更新表中的每个值:

create table #test (id int, address varchar(100))

insert into #test (id, address)
values (5, 'test address')

update Table1
set
    address = (select top 1 address from #test)
where exists (select 1 from #test t where id = t.id)

只会更新 id = 5 的条目:

create table #test (id2 int, address varchar(100))

insert into #test (id2, address)
values (5, 'test address')

update Table1
set
    address = (select top 1 address from #test)
where exists (select 1 from #test t where id = t.id2)

唯一的区别似乎是一个临时表的 ID 与源表匹配,而另一个则没有。这有什么关系?或者这里发生了什么更微妙或更明显的事情?

4

1 回答 1

2

显然这总是返回 true?

select 1 from #test t where id = t.id  

独立 id 正在使用 #test 的 t

在第二个 t 中没有 ID,因此它查找树到 Table1

消息是不要使用不明确的名称
如果名称不明确,则包括表(或别名)名称

你为什么要以那种方式使用存在?

于 2013-07-12T20:45:06.653 回答