我有一个plsql程序,它接受一个输入变量。这个变量(my_plsql_var)我需要决定select语句的where子句。如果我的变量是A,那么我需要查询一个特定的where子句,如果它是B,它使用特定的where子句,同样适用于C.我尝试了一些查询,但它们不起作用。我得到的最接近的是这个,但似乎存在语法错误,甚至不确定查询是否会产生我需要的内容。
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)
答案 0 :(得分:1)
这不仅可以简化到这个吗?
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')
);
答案 1 :(得分:0)
最好的办法是使用动态SQL ...
在字符串中构建select语句,然后使用execute immediate运行查询,如下所示。下面的代码未经测试,因此可能存在一些语法错误,但应该让您了解如何执行此操作。否则谷歌动态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;
答案 2 :(得分:0)
您需要评估输入并动态构建....
不确定你想要的程序是什么,但是这个只是打开一个参考光标,大概你会用它做点什么。
希望这有帮助。
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;
答案 3 :(得分:0)
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;