Why would someone use WHERE 1=1 AND in a SQL clause?

后端 未结 19 1995
甜味超标
甜味超标 2020-11-22 07:08

Why would someone use WHERE 1=1 AND in a SQL clause (Either SQL obtained through concatenated strings, either view definition)

I\'ve

相关标签:
19条回答
  • 2020-11-22 07:35

    I found usefull this pattern when I'm testing or doublechecking things on the database, so I can comment very quickly other conditions:

    CREATE VIEW vTest AS
    SELECT FROM Table WHERE 1=1 
    AND Table.Field=Value
    AND Table.IsValid=true
    

    turns into:

    CREATE VIEW vTest AS
    SELECT FROM Table WHERE 1=1 
    --AND Table.Field=Value
    --AND Table.IsValid=true
    
    0 讨论(0)
  • 2020-11-22 07:38

    I first came across this back with ADO and classic asp, the answer i got was: performance. if you do a straight

    Select * from tablename

    and pass that in as an sql command/text you will get a noticeable performance increase with the

    Where 1=1

    added, it was a visible difference. something to do with table headers being returned as soon as the first condition is met, or some other craziness, anyway, it did speed things up.

    0 讨论(0)
  • 2020-11-22 07:40

    Indirectly Relevant: when 1=2 is used:

    CREATE TABLE New_table_name 
    as 
    select * 
    FROM Old_table_name 
    WHERE 1 = 2;
    

    this will create a new table with same schema as old table. (Very handy if you want to load some data for compares)

    0 讨论(0)
  • 2020-11-22 07:42

    Actually, I've seen this sort of thing used in BIRT reports. The query passed to the BIRT runtime is of the form:

    select a,b,c from t where a = ?
    

    and the '?' is replaced at runtime by an actual parameter value selected from a drop-down box. The choices in the drop-down are given by:

    select distinct a from t
    union all
    select '*' from sysibm.sysdummy1
    

    so that you get all possible values plus "*". If the user selects "*" from the drop down box (meaning all values of a should be selected), the query has to be modified (by Javascript) before being run.

    Since the "?" is a positional parameter and MUST remain there for other things to work, the Javascript modifies the query to be:

    select a,b,c from t where ((a = ?) or (1==1))
    

    That basically removes the effect of the where clause while still leaving the positional parameter in place.

    I've also seen the AND case used by lazy coders whilst dynamically creating an SQL query.

    Say you have to dynamically create a query that starts with select * from t and checks:

    • the name is Bob; and
    • the salary is > $20,000

    some people would add the first with a WHERE and subsequent ones with an AND thus:

    select * from t where name = 'Bob' and salary > 20000
    

    Lazy programmers (and that's not necessarily a bad trait) wouldn't distinguish between the added conditions, they'd start with select * from t where 1=1 and just add AND clauses after that.

    select * from t where 1=1 and name = 'Bob' and salary > 20000
    
    0 讨论(0)
  • 2020-11-22 07:45

    where 1=0, This is done to check if the table exists. Don't know why 1=1 is used.

    0 讨论(0)
  • 2020-11-22 07:46

    Just adding a example code to Greg's answer:

    dim sqlstmt as new StringBuilder
    sqlstmt.add("SELECT * FROM Products")
    sqlstmt.add(" WHERE 1=1") 
    
    ''// From now on you don't have to worry if you must 
    ''// append AND or WHERE because you know the WHERE is there
    If ProductCategoryID <> 0 then
      sqlstmt.AppendFormat(" AND ProductCategoryID = {0}", trim(ProductCategoryID))
    end if
    If MinimunPrice > 0 then
      sqlstmt.AppendFormat(" AND Price >= {0}", trim(MinimunPrice))
    end if
    
    0 讨论(0)
提交回复
热议问题