如何根据输入变量运行不同的查询?

2022-03-09 00:00:00 oracle plsql oracle-sqldeveloper

我必须根据传入变量的值从单个表返回不同的列。

我已尝试使用CASE语句,但不起作用。是否有办法实现此逻辑?

select
    case 
        when :var1='A1' then (select variable,value1,value2 from table where variable='A1')
        when :var1='A2' then (select variable,value3,value4 from table where variable='A2')
    end
from dual;

解决方案

将案例移至SELECT:

SELECT someVariable AS VARIABLE_NAME,
       CASE someVariable
         WHEN 'A1' THEN VALUE1
         WHEN 'A2' THEN VALUE3
         ELSE NULL
       END AS FIRST_VALUE,
       CASE someVariable
         WHEN 'A1' THEN VALUE3
         WHEN 'A2' THEN VALUE4
         ELSE NULL
       END AS SECOND_VALUE
  FROM TABLE

相关文章