Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SUBSELECT contains an error - but sql statement continues

I have the following example code:

create table #tempmembers (
    memberid int
)

update Members set 
Member_EMail = NULL
where Member_ID in (select member_id from #tempmembers)

The subselect contains an error, since #tempmembes does not contain a column named member_id, but the sql statements run WITHOUT any errors and update no rows.

If I then add just ONE row to #tempmembers:

create table #tempmembers (
    memberid int
)
insert into #tempmembers select 1

update Members set 
Member_EMail = NULL
where Member_ID in (select member_id from #tempmembers)

it still runs without any errors - but this time ALL records in Members will be affected.

Why does the SQL statement not fail completely? And if the failing subselect is evaluated to NULL - should updating all rows in Members not only occur if it had been:

update Members set 
Member_EMail = NULL
where Member_ID not in (select member_id from #tempmembers)
like image 705
Kresten Buch Avatar asked Feb 13 '26 10:02

Kresten Buch


1 Answers

It's inheriting member_id from the outer query so is equivalent to:

   ...
   (select Members.member_id from #tempmembers)

This will fail as expected:

   ...
   (select #tempmembers.member_id from #tempmembers)
like image 144
Alex K. Avatar answered Feb 16 '26 02:02

Alex K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!