Create table tb1 (id int identity (1, 1), name varchar (10), remark varchar (50) default '');
Create table tb2 (xid int identity (1, 1), name varchar (10 ));
Insert into tb1 (name)
Select 'A'
Union all select 'B'
Union all select 'C'
Insert into tb2 (name) select 'x'
Select * from tb1 where id in (select id from tb2)
Select * from tb1 where id in (select xid from tb2)
Drop table tb1
Drop table tb2;
The result is:
Id name remark
-----------------------------------------------------------------------
1
2 B
3 c
Id name remark
-----------------------------------------------------------------------
1
After executing this script, you can find the problem:
Why does the select * from tb1 where id in (select id from tb2) Perform a syntax check on the subquery statements in the condition during execution? What is the default value equivalent to 1 = 1?
In addition, select * from tb1 where id in (select id from tb2 where id = 1) will follow select * from tb1 where id in (select xid from tb2 where xid = 1) this shows that the syntax of the subquery statement is not checked, but filtering plays a role, which is indeed a strange thing.
If I mistakenly typed select xid in select * from tb1 where id in (select xid from tb2) into select id when I typed an SQL statement, then there will be a problem with the Retrieved Data (full extraction), but there is no prompt, because the syntax check is skipped. Therefore, this is a potential pitfall when writing SQL statements.
It is said that there are similar problems in oracle and there is no trial.
You should be cautious when writing SQL scripts, because this morning I encountered such a problem in SQL. Remember the lessons.