If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > where 1=1!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-11, 07:11
RRam29 RRam29 is offline
Registered User
 
Join Date: Apr 2011
Location: India
Posts: 3
where 1=1!

Hello All,

A query has come in for execution which goes like this:

select ....
from ....
where 1=1
and .....

I am not sure what the actual use of the part "where 1=1" is in a query, be it simple or complex.
Do we really have advantages when using this where clause?
Does it make a visible difference in the response we get for the query?
Or is it just a bit of sloppy coding?

Would like to know your opinions.

Thankyou.

And a good day to all!!
Reply With Quote
  #2 (permalink)  
Old 04-21-11, 08:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
as you guessed, the 1=1 condition does not alter the results of the query

(in fact, the optimizer will likely remove it anyway)

however, it is ~very~ useful when building a query

consider this scenario -- a web form has a number of form fields, and each form field can either have a value in it, or, if left blank, represents a column that is not to be searched

of course, if all the fields are left blank, the search is supposed to return all rows

so as the application logic processes the form fields, it must decide which is the first non-empty field, and this becomes the first condition in the WHERE clause

thus, it has to decide when to generate the WHERE keyword, and when to generate the AND keyword for a subsequent condition

and of course, if all the fields are left blank, then the WHERE clause isn't really needed

if you've ever built up a query string like this, you know that the code to decide whether to use WHERE or AND in front of each form fields gets complex and repetitive

resulting in application code bloat

now consider a query that has WHERE 1=1 already built into it

for each non-empty form field, you just append another AND

at the end, if all the form fields were empty, the entire WHERE clause is 1=1, which is always true for all rows, returning everything, as expected


make more sense now?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-23-11, 03:03
RRam29 RRam29 is offline
Registered User
 
Join Date: Apr 2011
Location: India
Posts: 3
Yes, that makes sense.

Thankyou!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On