In SQL SERVER, the syntax of the subquery statement in the condition statement is not checked

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.