Whats the exact meaning of having a condition like where 0=0?

user2622662 picture user2622662 · Aug 19, 2013 · Viewed 13.3k times · Source

I'm having a bit confusion in using a statement like "where 0=0" in Oracle procedures? Can someone please clarify it? Even though we do it for dynamic selection, why do we add this statement even though we append the actual condition in the query? Will this where condition make any difference to the result set?.. I went through How can I Select all rows where column contain any words of a string? but I dint exactly understand the reason for using "where 0=0". Can some one please give me the proper reason for using such a condition?..

Thanks in Advance..;)

Answer

Dmitry Bychenko picture Dmitry Bychenko · Aug 19, 2013

We use 0 = 0 or, usually, 1 = 1 as a stub:

select *
  from My_Table
 where 1 = 1

So when you write filters you can do it by adding/commenting out single lines:

-- 3 filters added
select *
  from My_Table
 where 1 = 1
   and (Field1 > 123) -- 1st
   and (Field2 = 456) -- 2nd 
   and (Field3 like '%test%') -- 3d

Next version, say, will be with two filters removed:

-- 3 filters added, 2 (1st and 3d) removed
select *
  from My_Table
 where 1 = 1
   -- and (Field1 > 123) -- <- all you need is to comment out the corresponding lines
   and (Field2 = 456)
   -- and (Field3 like '%test%')

Now let's restore the 3d filter in very easy way:

-- 3 filters added, 2 (1st and 3d) removed, then 3d is restored
select *
  from My_Table
 where 1 = 1
   -- and (Field1 > 123) 
   and (Field2 = 456)
   and (Field3 like '%test%') -- <- just uncomment