如果ref cursor的First为空,PLSQL将执行另一个Select语句

如果ref cursor的First为空,PLSQL将执行另一个Select语句,sql,oracle,plsql,Sql,Oracle,Plsql,我正在尝试做类似以下的事情: procedure some_name ( p_ref_cursor in out nocopy sys_refcursor ) is begin open p_ref_cursor for select item1, item2 from (some complicated join query); fetch p_ref_cursor into xx; if p_ref_cursor%NOTFOUND then select item1, item2 f

我正在尝试做类似以下的事情:

procedure some_name ( p_ref_cursor in out nocopy sys_refcursor )

is

begin

open p_ref_cursor for
select item1, item2 from (some complicated join query);

fetch p_ref_cursor into xx;
if p_ref_cursor%NOTFOUND
then
select item1, item2 from table2 where item3='y';
end if;

end
procedure some_name ( p_ref_cursor in out nocopy sys_refcursor )

is
local_var1 number
local_var2 number
begin

open p_ref_cursor for
select item1, item2 
into local_var1, local_var2
from (some complicated join query);

if local_var1 is null
then
select item1, item2 from table2 where item3='y';
end if;

end
如果第一个select语句未返回任何内容,则应执行第二个select语句以检索值。我遇到的问题是,当提取光标时,结果集被清除。因此,无论第一个select语句是否找到任何内容,它都会表现为未找到

我尝试了以下方法:

procedure some_name ( p_ref_cursor in out nocopy sys_refcursor )

is

begin

open p_ref_cursor for
select item1, item2 from (some complicated join query);

fetch p_ref_cursor into xx;
if p_ref_cursor%NOTFOUND
then
select item1, item2 from table2 where item3='y';
end if;

end
procedure some_name ( p_ref_cursor in out nocopy sys_refcursor )

is
local_var1 number
local_var2 number
begin

open p_ref_cursor for
select item1, item2 
into local_var1, local_var2
from (some complicated join query);

if local_var1 is null
then
select item1, item2 from table2 where item3='y';
end if;

end
但是,看起来INTO语句不适用于ref cursor

谢谢,
Dan这似乎是对我认为应该有效的东西的过度杀戮。但是我相信它可以解决这个问题,并且消除对if语句的需要。此外,它可能会稍微慢一些,因为它必须同时执行两个查询。所以,如果性能是最重要的,那么我不会使用这个

WITH CTE AS
(SELECT item1, item2, '1' as Src 
FROM (some complicated join query)
UNION
SELECT item1, item2, '2' as SRC
FROM table2 where item3='y')
SELECT item1, item2
FROM CTE
WHERE src = (Select min(src) from cte)

谢谢你的回答。绩效是一个问题,因此我不想参加工会:(尝试使用
UNION ALL
而不是
UNION
UNION
消除结果集中的重复项,这可能非常耗时;
UNION ALL
省去了重复检查,通常提供更好的性能。共享和享受。这是直接复制吗?信息与输入?是的,这是一个输入错误。这是输入错误。)我最后做的就是返回两个ref游标。谢谢。