3

Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?

I've been tasked with reviewing some SQL stored procedures and have seen many that look like the following:

SELECT 
  X, Y, Z 
FROM 
  Table
WHERE 
  1 = 1
ORDER BY
  X

Why would someone use '1 = 1' for the where clause?

Thanks!

Community
  • 1
  • 1
derivation
  • 4,362
  • 3
  • 27
  • 23

2 Answers2

8

It's common in dynamic SQL, in order to append additional criteria to a WHERE clause. Otherwise, it's useless and it is ignored by the optimizer.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
2

possibly to dynamically add conditions to the where clause.

zerodin
  • 857
  • 5
  • 9