像0 = 0这样的条件的确切含义是什么? [英] Whats the exact meaning of having a condition like where 0=0?

查看:525
本文介绍了像0 = 0这样的条件的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Oracle过程中使用诸如"where 0 = 0"之类的语句时,我有些困惑吗?有人可以澄清一下吗?即使我们将其用于动态选择,即使在查询中附加了实际条件,为什么还要添加此语句?条件是否会影响结果集? 我经历了 如何选择列包含字符串中的任何单词的所有行? 但我完全理解使用"where 0 = 0"的原因. 有人可以给我使用这种条件的正确理由吗?..

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?..

在此先感谢..)

推荐答案

在使用动态sql时,可能需要添加额外的子句,具体取决于满足某些条件. 1 = 1子句在查询中没有任何意义(除了始终满足),它的唯一用途是降低用于生成查询的代码的复杂性.

When using dynamic sql, extra clauses may need to be added, depending upon certain conditions being met. The 1=1 clause has no meaning in the query ( other than it always being met ), its only use is to reduce the complexity of the code used to generate the query in the first place.

例如这个伪代码

DECLARE
v_text VARCHAR2(2000) := 'SELECT * FROM table WHERE 1=1 ';
BEGIN

    IF condition_a = met THEN
    v_text := v_text ||' AND column_1 = ''A'' ';
    END IF;

    IF condition_b = also_met THEN
    v_text := v_text ||' AND column_2 = ''B'' ';
    END IF;

execute_immediate(v_text);

END;

比下面的伪代码更简单,并且随着添加更多的子句,它只会变得更加混乱.

is simpler than the pseudo code below, and as more clauses were added, it would only get messier.

DECLARE
v_text VARCHAR2(2000) := 'SELECT * FROM table  ';
BEGIN

    IF condition_a = met THEN
    v_text := v_text ||' WHERE column_1 = ''A'' ';
    END IF;

    IF condition_b = also_met AND 
       condition_a != met THEN
    v_text := v_text ||' WHERE column_2 = ''B'' ';
    ELSIF condition_b = also_met AND 
       condition_a = met THEN
    v_text := v_text ||' AND column_2 = ''B'' ';
    END IF;

execute_immediate(v_text);

END;

这篇关于像0 = 0这样的条件的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆