Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where 0 = 0

Status
Not open for further replies.

albertkao

Programmer
Feb 3, 2010
5
0
0
CA
An old program has this statement
select * from db1 where 0 = 0

Why "where 0 = 0" is needed?
 
Does it work without the WHERE clause ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Could be many reasons. Could be that if the program is or was concatenating other criteria to the where clause, they didn't want the application logic to figure out whether or not to put the word WHERE or the word AND (or even OR).

Consider the following pseudocode:
Code:
String sql = "select * from db1 where 0 = 0"
If CheckBox1 Is Checked Then sql = sql + " AND Col1 =" + TextBox1
If CheckBox2 Is Checked Then sql = sql + " AND Col2 =" + TextBox2
 
WHERE 0=0 (or the more frequently seen WHERE 1=1) is used exactly as riverguy says, to append optional additional AND conditions

to append OR conditions, however, you'd use WHERE 0=1

:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
RiverGuy and r937 are correct in why such a thing might occur but it may not be the best of approaches. Things like 0=0 or 0=1 will be evaluated on every record even though the result never changes. That can slow execution. If concatenation of clauses is required then something like (in no particular language) ...
Code:
If WhereClause = "" Then
   WhereClause = "WHERE " & SubClause
Else
   WhereClause = WhereClause & " AND " & SubClause
End If
avoids the use of dummy WHERE conditions.
 
Things like 0=0 or 0=1 will be evaluated on every record
database optimizers are smart enough to "optimize away" those types of conditions


heck, even mysql can do that ;-)


your if/else code block would have to be applied to every form field

too many trees, not enough forest

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top