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

前端 未结 3 973
温柔的废话
温柔的废话 2021-01-02 23:50

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 negati

相关标签:
3条回答
  • 2021-01-03 00:34

    They may have built the query from substrings.

    It may be a way to build it with just ANDs, depending on the business rule match, so you don't really have to care which is your first condition.

    Wouldn't do it that way myself.

    0 讨论(0)
  • 2021-01-03 00:41

    If they are building the query dynamically, you should check whether they're using bind variables. Building the query from literals requires extra parsing, potentially limiting scalability, and also can greatly increase the risk of SQL Injection attacks.

    where 1 = 1 and my_id = :b1;
    

    (and then defining the value of the bind variable)

    is generally much better than:

    where 1 = 1 and my_id = 123456;
    
    0 讨论(0)
  • 2021-01-03 00:45

    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.

    0 讨论(0)
提交回复
热议问题