选择输入无效字段

时间:2018-12-28 15:10:41

标签: sql sql-server

(请原谅所有不良做法元素)。下面的代码将所有标志设置为1。我知道为什么这样做,但是应该这样做,因为我没有资格使用Table1中的mykey。

declare @table1 table (mykey varchar(20), myflag bit)

declare @table2 table (mykey2 varchar(20)) 

insert into @table1
select 'A',0

insert into @table1
select 'B',0

insert into @table2
select 'Z'

update @table1
set myflag = 1 
where mykey in (select mykey from @table2) 

Select * from @table1

我希望不要执行sql,因为mykey在table2上不存在。当table2有一个名为mykey的字段时,您可以设置mykey所在的语句(从@ table2中选择mykey)而无需任何限定。我正在使用SQL Server 2017

0 个答案:

没有答案