SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 2=2
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

seeker62
Starting Member

40 Posts

Posted - 11/12/2012 :  15:37:43  Show Profile  Reply with Quote
This is very basic I know but when 2=2 is found in the where clause what does it mean.

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/12/2012 :  15:58:41  Show Profile  Reply with Quote
2=2 evaluates to TRUE, so it has no effect on the query.

Usually people do that when they want to construct a query programmatically.
If you start with 1=1 or 2=2, then all the additional clauses that you may want to add can be prefixed with an AND. For example:

SELECT
	col1,col2
FROM
	SomeTable
WHERE
	1 = 1
	AND StateCode = 'CT' --- dynamically added sections
	AND ZipCode = '06901'
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22772 Posts

Posted - 11/17/2012 :  03:58:53  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You see that kind of code only when dynamic sql is used and the expressions in the where clause are added based on condition. But in most cases you can avoid it by using conditional where clause like below

WHERE
(col1=@param and @flag=1)
or
(col2=@param and @flag=2)
or
.
.
.

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000