我正在尝试执行以下操作:将status列设置为1第二个表中第一个表(变量)中的行不存在时。
我尝试了这个:
update @table1
set status=1
where NOT EXISTS (select top 1 1 from @table2 where @table1.foo=@table2.foo)
Run Code Online (Sandbox Code Playgroud)
但这甚至无法编译,无法@table1在Where语句中识别。
必须声明标量变量“ @ table1”。
有什么线索吗?
您的方法很好。您只需要表别名,因为@SQL Server中的用来表示变量(标量或表),因此别名存在问题:
update t1
set status = 1
from @table1 t1
where not exists (select 1 from @table2 t2 where t2.foo = t1.foo);
Run Code Online (Sandbox Code Playgroud)
注意,top 1在子查询中不必要。