1

(Please forgive all the bad practice elements).The code below results in all flags set to 1. I understand why but should it have done that as I did not qualify I wanted to use mykey from table1.

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

I would have expected the sql not to be executed as mykey does not exist on table2. When table2 has a field called mykey you can set the statement where mykey in (select mykey from @table2) without any qualification. I am using SQL Server 2017

Pat
  • 11
  • 2
  • Not sure what the question is here... When you need to qualify columns or when you get an error if you don't? When they're ambiguous, which they aren't here.Is it advisable to do so even if they're not ambiguous? Yes. You got an example here why this can be a good thing. – sticky bit Dec 28 '18 at 15:18

0 Answers0