1=1 Usage and performance

Source: Internet
Author: User

1=1 usage and performance

Fuse: Found some where and JOIN on statements with 1=1 conditions in the optimized stored procedure .

Why do you use the WHERE 1=1

to facilitate the stitching of SQL conditions :

Instance:

DECLARE @SQL1 NVARCHAR (max), @SQL2 NVARCHAR (max), @CLASSID intset @CLASSID =2select @SQL1 = ' SELECT * from w_question WHERE 1 = 1 ' IF ISNULL (@CLASSID, 0) <>0select @[email protected]+ ' and classid= ' +convert (NVARCHAR), @CLASSID)

The final splicing statement is:

SELECT * from W_question WHERE 1=1 and classid=2

If not the conditions of the 1=1 will appear:

SELECT * from W_question WHERE and classid=2

Or:

SELECT * from W_question WHERE

Such an SQL statement, causing an error

WHERE 1=1 has an impact on performance

because the query parser filtered the 1=1 directly in the algebraic tree optimization phase. , so 1=1 has no effect on performance.

Instance:

SELECT * from W_questionselect * from W_question WHERE 1=1


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/E1/wKiom1WsY_nAgFmoAAF-PJhDBG4676.jpg "title=" Picture 1.png "alt=" Wkiom1wsy_nagfmoaaf-pjhdbg4676.jpg "/>

This article is from the "Sukun" blog, make sure to keep this source http://sukunwu.blog.51cto.com/10453116/1676267

1=1 Usage and performance

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.