Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Oracle, is starting the SQL Query's WHERE clause with 1=1 useful?

Tags:

sql

oracle

I'm working with a client that starts almost all of their WHERE clauses in Oracle with 1=1. Forgive my ignorance, but isn't this a no-op? Are there any negative consequences of this usage?

Here's a scrubbed example:

SELECT   gpz.zname
         ,gpp.pname
FROM     table1 gpp INNER JOIN table2 gpz ON gpz.p_id = gpp.p_id
WHERE    1=1
         AND gpp.active = 1
         AND gpz.active = 1
like image 480
Blanthor Avatar asked Jul 01 '09 14:07

Blanthor


1 Answers

It's done to simplify dynamic SQL generation. Basically each condition can be added as AND <condition> without treating the first condition as special (it's preceded by WHERE not AND) or even worrying if there should be a WHERE clause at all.

So just write it off as easy of use or, arguably, laziness.

like image 69
cletus Avatar answered Sep 28 '22 03:09

cletus