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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHERE 1=1

Status
Not open for further replies.

DbHd

Programmer
Dec 20, 2004
39
What is the significance of the WHERE clause in the example below? (There is no column named "1")

SELECT * from vwAcme WHERE 1=1

 
The expression (1 = 1) will always evaluate as true, hence the SQL is effectively the same as:

SELECT * FROM vwAcme


For interest, I have used such a clause when compiling SQL statments dynamically in VB code for use subsequent use as a form recordsource. I build up a WHERE clause based on user selections from another form like so:

strWhere = "WHERE (1=1)"
For Each *condition* In *list of possible conditions*
If *condition is selected* Then
strWhere = StrWhere & " AND " & *condition*
End If
Next *condition*

The (1=1) eliminiates the need to test in the code whether a condition is the first to be added the clause and hence don't add an "AND" at the front.

Cheers,
HappyCoda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top