Use of 1=2 in a SQL query

There is a good use for this 1=2 part of the WHERE clause if you are creating a table from another, but you don't want to copy any rows. For example:

CREATE TABLE ABC_TEMP AS
    SELECT * FROM ABC WHERE 1=2;

when T.Active = 'N' and 1=2 then 'Not Working Anymore' 

Simple, the above condition will never become true. So the result will always be C.Country_Name


It is a common trick used in dynamic construction of SQL filter clauses. This allows the automated construction of "T.Active = 'N' and" with no check needed for a following clause, because "1=2" will always be appended.

Update: Whether 1=1 or 1=2 is used depends on whether conjunctive or disjunctive normal form is supposed to be used in building the automated clauses. In this case, there seems to have been a mismatch of design and implementation.

Update 2 I believe most developers prefer conjunctive normal form, with major terms joind by AND, but disjunctive normal form is equal in expressive power and size of code.

Tags:

Sql