Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

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.

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:

	1 = 1
	AND StateCode = 'CT' --- dynamically added sections
	AND ZipCode = '06901'
Go to Top of Page

Premature Yak Congratulator

22864 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

(col1=@param and @flag=1)
(col2=@param and @flag=2)


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next 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.19 seconds. Powered By: Snitz Forums 2000