简体   繁体   English

基于过程输入条件的条件式子句Oracle SQL

[英]Conditional Where Clause Oracle SQL based on procedure input varaible

I have a plsql procedure which take an input variable. 我有一个带输入变量的plsql过程。 This variable (my_plsql_var) I need to decide the where clause of my select statement. 我需要确定此变量(my_plsql_var)的select语句的where子句。 If my variable is A then I need to query a specific where clause, if it is B it uses that specific where clause and same goes for C. I have tried some queries but they do not work. 如果我的变量是A,那么我需要查询一个特定的where子句,如果它是B,它将使用该特定的where子句,C也一样。我尝试了一些查询,但它们不起作用。 The closest I have gotten is this, but there seems to be syntax errors and am even unsure if the query will produce what I need. 我得到的最接近的是这个,但是似乎存在语法错误,甚至不确定查询是否会产生我所需要的。

SELECT ID, 
    CASE(CAST WHEN my_col1 in ('A') and my_col2 = 'A' then 'A GROUP'
                             WHEN my_col1 in ('B') and my_col2 = 'B' then 'B GROUP'
                             WHEN my_col1 in ('C') and my_col2 = 'C' then 'C GROUP'
                             else null
        end as varachar2)) as my_awesome_col
        FROM
        my_table
        WHERE
        id= 100 and
        name = 'Smith' and 
        CASE (WHEN my_plsql_var = 'A' then my_col1 in ('A') and my_col2 = 'A'
             WHEN my_plsql_var = 'B' then my_col1 in ('B') and my_col2 = 'B' and my_special_col = 'B'
             WHEN my_plsql_var = 'C' then my_col1 in ('C') and my_col2 = 'C'
        end as varachar2)

Can this not just be simplified to this? 不仅可以简化为这个吗?

SELECT ID,
    my_plsql_var || ' GROUP' AS Group
FROM my_table
WHERE ID = 100
    AND NAME = 'Smith'
    AND (
           (my_plsql_var = 'A' AND my_col1 IN ('A') AND my_col2 = 'A')
        OR (my_plsql_var = 'B' AND my_col1 IN ('B') AND my_col2 = 'B' AND my_special_col = 'B')
        OR (my_plsql_var = 'C' AND my_col1 IN ('C') AND my_col2 = 'C')
    );

Your best bet for this is to use dynamic SQL... 最好的选择是使用动态SQL ...

Build up your select statement in a string, then use execute immediate to run the query as below. 在字符串中建立select语句,然后使用立即执行以如下方式运行查询。 The code below is untested so might have some syntax errors but should give you an idea of how to do it. 以下代码未经测试,因此可能会有一些语法错误,但是应该使您了解如何执行此操作。 Otherwise Google dynamic SQL. 否则,Google动态SQL。

my_sql_string := 'SELECT ID, 
                  CASE(CAST WHEN my_col1 in (''A'') and my_col2 = ''A'' then ''A GROUP'' 
                       WHEN my_col1 in (''B'') and my_col2 = ''B'' then ''B GROUP''
                       WHEN my_col1 in (''C'') and my_col2 = ''C'' then ''C GROUP''
                       else null
        end as varachar2)) as my_awesome_col
        FROM
        my_table
        WHERE
        id= 100 and
        name = ''Smith'' and ';

 if my_plsql_var = 'A' then
  my_sql_string := my_sql_string || 'my_col1 in (''A'') and my_col2 = ''A''';
 else if my_plsql_var = 'B' then 
  my_sql_string := my_sql_string || 'my_col1 in (''B'') and my_col2 = ''B''';
 else if my_plsql_var = 'C' then
  my_sql_string := my_sql_string || my_col1 in (''C'') and my_col2 = ''C''';
 end if;
v_output := execute immediate my_sql_string;

You need to evaluate your inputs and build dynamically.... 您需要评估您的输入并动态构建。

Not certain what you want the procedure to do but this one just opens a ref cursor presumably you would do something with it. 不确定您希望该过程执行什么操作,但是此操作仅会打开一个ref游标,可能您会对此进行操作。

Hope this helps. 希望这可以帮助。

    create or replace procedure my_plsql_procedure
    (
        my_plsql_var in varchar2
    )
    is
        dataset sys_refcursor;
        strSql CLOB;
        strPred VARCHAR2(500);
        bAddOtherPred boolean := my_plsql_var = 'B';
    begin
        if bAddOtherPred then
            strPred :=  q'~ and my special_col = 'B' ~';
        else
            strPred := null;
        end if;

        strSql := q'~ 
            select id,
                   CASE when my_col1 = myCol2 and my_col1 = 'A' THEN 'A GROUP'
                        when my_col1 = myCol2 and my_col1 = 'B' THEN 'B GROUP'
                        when my_col1 = myCol2 and my_col1 = 'C' THEN 'C GROUP'
                        else null end as my_awesome_col
            from my_table
            where my_col1 = my_col2
            and my_col1 = :my_plsql_var 
            and id = 100
            and name = 'Smith' ~' || strPred;

        open dataset 
        for strSql
        using my_plsql_var;

    end;
        Hello you need to build the SELECT Clause dynamically based upon your input. Below is the example for this.


        CREATE OR REPLACE PROCEDURE TEST1_DYN(
        p_in IN VARCHAR2,
        p_ref OUT sys_refcursor )
    AS
      lv_select LONG;
    BEGIN
      lv_select:='SELECT ID,     
                  (CASE WHEN my_col1 in ('''||p_in||''')'|| 'and my_col2 = '''||p_in||''''||' then '||''''||p_in||' GROUP'''||
                  ' else null        
                  end)  my_awesome_col        
                  FROM        
                  my_table        
                  WHERE        
                  id= 100 and        
                  name = ''Smith'' and         
                  my_plsql_var = '||''''||p_in||''''||' then my_col1 in ('||''''||p_in||''''||') and my_col2 = '||''''||p_in||'''             
                  ';
      dbms_output.put_line(lv_select);
OPEN p_ref for lv_select;
    END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM