在Oracle中,以1 = 1开头SQL查询的WHERE子句有用吗? [英] In Oracle, is starting the SQL Query's WHERE clause with 1=1 useful?
问题描述
我正在与一个使用1=1
开始Oracle中几乎所有WHERE子句的客户端一起工作.原谅我的无知,但这不是禁忌吗?这种用法会带来负面影响吗?
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
推荐答案
这样做是为了简化动态SQL生成.基本上,每个条件都可以添加为AND <condition>
,而无需将第一个条件视为特殊条件(它以WHERE
而不是AND
开头),甚至可以担心是否应该有一个WHERE
子句.
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.
这篇关于在Oracle中,以1 = 1开头SQL查询的WHERE子句有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!