2

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,022
  • 3
  • 25
  • 23

2 Answers2

7

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
  • 314,254
  • 77
  • 507
  • 490
2

possibly to dynamically add conditions to the where clause.

zerodin
  • 839
  • 5
  • 8