SQL Oracle UNION 只有一个参数

SQL Oracle UNION with only a parameter

我在 ORACLE 中有这个查询 SQL:

select W.x, 'RESULT ' || W.x from W where W.z=?
   union select to_number(W.x || '0000'),'RESULT ' || W.x from W where W.z=?
   union select to_number(W.x || '1000'),'RESULT ' || W.x from W where W.z=?
   union select to_number(W.x || '2000'),'RESULT ' || W.x from W where W.z=?
   union select to_number(W.x || '3000'),'RESULT ' || W.x from W where W.z=?
   union select to_number(W.x || '4000'),'RESULT ' || W.x from W where W.z=?
order by 1

我想只传递一个相同的参数,因为它将用于只有一个参数的特殊解决方案。

我的意思是,只有 ? 具有相同的值,但 UNION 必须存在。

所有值都是数字。

您评论说您可能正在使用我不知道的工具 (Intellij DataGrid)。

但是,如果稍微修改一下查询,那么

  • 您当前的查询用作内联视图
  • 向其中添加一个新的 - z - 列
  • 将别名应用于要返回的第二列 (result)
  • where 子句移出子查询

那么这个可能有效:

select x, result
from 
(
select           W.x,           'RESULT ' || W.x result, W.z from W union all
select to_number(W.x || '0000'),'RESULT ' || W.x       , W.z from W union all
select to_number(W.x || '1000'),'RESULT ' || W.x       , W.z from W union all
select to_number(W.x || '2000'),'RESULT ' || W.x       , W.z from W union all
select to_number(W.x || '3000'),'RESULT ' || W.x       , W.z from W union all
select to_number(W.x || '4000'),'RESULT ' || W.x       , W.z from W  
)
where z = ?
order by 1;

除此之外,如果您使用 SQL*Plus,您会

select           W.x,           'RESULT ' || W.x from W where W.z=&&par_z union all
select to_number(W.x || '0000'),'RESULT ' || W.x from W where W.z=&&par_z union all
select to_number(W.x || '1000'),'RESULT ' || W.x from W where W.z=&&par_z union all
select to_number(W.x || '2000'),'RESULT ' || W.x from W where W.z=&&par_z union all
select to_number(W.x || '3000'),'RESULT ' || W.x from W where W.z=&&par_z union all
select to_number(W.x || '4000'),'RESULT ' || W.x from W where W.z=&&par_z 
order by 1;

运行 它,系统会提示您输入 par_z 值并得到结果。在两个后续查询调用之间,您必须 undefine par_z 以便它请求一个新值。


或者,它可以是 returns 的函数,例如参考:

create or replace function f_test (par_z in number) 
  return sys_refcursor
is
  l_rc sys_refcursor;
begin
  open l_rc for
    select           W.x,           'RESULT ' || W.x from W where W.z=par_z union all
    select to_number(W.x || '0000'),'RESULT ' || W.x from W where W.z=par_z union all
    select to_number(W.x || '1000'),'RESULT ' || W.x from W where W.z=par_z union all
    select to_number(W.x || '2000'),'RESULT ' || W.x from W where W.z=par_z union all
    select to_number(W.x || '3000'),'RESULT ' || W.x from W where W.z=par_z union all
    select to_number(W.x || '4000'),'RESULT ' || W.x from W where W.z=par_z 
    order by 1;
end;

你可以这样称呼它

select f_test(10) from dual;

另一个更有效的解决方案是这样的。

我假设 W.x 是一个数字,但从您的原始查询中并不清楚。 如果不是,则在 with 子句中添加 to_number(W.x)。

with src as
  ( select W.x as x from W where W.z = ?
  )
select src.x               , 'RESULT ' || to_char(src.x) from src union all
select src.x * 10000       , 'RESULT ' || to_char(src.x) from src union all
select src.x * 10000 + 1000, 'RESULT ' || to_char(src.x) from src union all
select src.x * 10000 + 2000, 'RESULT ' || to_char(src.x) from src union all
select src.x * 10000 + 3000, 'RESULT ' || to_char(src.x) from src union all
select src.x * 10000 + 4000, 'RESULT ' || to_char(src.x) from src
order by 1
;