图片我在包中有以下查询:
SELECT col1 FROM table WHERE id=5;
SELECT otherCol FROM otherTable WHERE id=78;
我想返回一条记录,其中两列包含值'col1'和'otherCol'。
在我的情况下,我会有很多子查询和DECODE
语句,所以为了便于阅读,我想将它分成不同的查询ala:
var result = {}; -- Unfortunately PL/SQL doesn't cope very well with JavaScript.
SELECT col1 INTO someVar FROM table WHERE id=5;
IF someVar = 1 THEN
result.X = SomeQuery();
ELSE
result.X = SomeEntirelyDifferentQuery();
END IF;
SELECT col INTO result.Z FROM tab1;
SELECT coz INTO result.Q FROM tab2;
IF result.Z IS NULL THEN
result.B = Query1();
ELSE
result.B = Query2();
END IF;
... and so on.
RETURN result;
所以基本上我想创建一个“空记录”,然后根据一些条件为该记录中的列分配不同的值
这可以通过DECODE解决,但结果查询既不是非常易读也不是非常高效。
答案 0 :(得分:1)
您必须定义函数可以返回的对象类型。
CREATE OR REPLACE TYPE fancy_function_result_row as object
(
X number,
Y number,
Q date,
Z varchar2(30)
);
create or replace function fancy_function(...)
return fancy_function_result_row
as
my_record fancy_function_result_row := fancy_function_result_row();
begin
my_record.X := 1;
etc...
end;
如果要将记录插入表中,只需定义该表的行类型就可以轻松得多。
declare
row_my_table my_table%rowtype;
begin
row_my_table.X := etc..
insert into my_table values row_my_table;
end;
答案 1 :(得分:0)
- 连接值'col1'和'otherCol':
with r1 as (select col1 from table where id=5),
r2 as (select otherCol from otherTable WHERE id=78),
select r1.col1 || ' concat with ' || r2.othercol from r1, r2
- 使用DECODE执行此条件:
SELECT DECODE (col1,
1,
(SELECT 1 FROM query1),
(SELECT 1 FROM DifferentQuery)
)
FROM TABLE