2013-08-05 132 views
2

我有一个游标,其中的值来自select,我想在执行某些操作之后执行某些操作,具体取决于我是否找到任何行或找不到任何行。检查SYS_REFCURSOR是否为空的最佳方法

recs_Table SYS_REFCURSOR; 

begin 

    open recs_Table for 
     select * from table1, table2; 


    if recs_Table%found then 
     --do this 
    else 
     --do that 
    end if; 

end; 

这似乎没有工作,任何帮助吗?泰

+3

您需要检查您的查询,打开,提取,关闭游标。请参阅oracle docs:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#LNPLS00605 – Art

回答

4

您需要使用FOUND属性%之前执行FETCH反光标。你的代码更改为类似

DECLARE 
    recs_Table SYS_REFCURSOR; 
    nTable_1_value NUMBER; 
    nTable_2_value NUMBER; 
begin 

    open recs_Table for 
     select * from table1, table2; 


    FETCH recs_Table INTO nTable_1_value, nTable_2_value; 

    if recs_Table%found then 
     --do this 
    else 
     --do that 
    end if; 

end; 

请注意,你可能需要一个外部变量到FETCH语句,一个在Table 1和Table每一列的INTO子句的方式。还要注意,这个游标写入的方式可能会得到比您预期的更多的行;因为没有指定连接条件,您将得到所谓的笛卡尔连接,其中TABLE1中的每一行都连接到TABLE2中的每一行 - 因此,您将返回的行数是(TABLE1中的行数)* (TABLE2中的行数)。

一个潜在的更简单的方式来做到这将是使用游标FOR循环,如下:

DECLARE 
    bData_found BOOLEAN := FALSE; 
begin 
    FOR aRow IN (select * from table1, table2) 
    LOOP 
    -- If the program gets here, it means a row was fetched 

    -- do this 

    bData_found := TRUE; 

    EXIT; -- if you only care if data was found and don't want to 
      -- process all the rows 
    END LOOP; 

    IF NOT bData_found THEN 
    -- do that 
    END IF; 
end; 

分享和享受。

1

我们用两个程序来执行结果

create or replace procedure pro_sample(recs_Table out SYS_REFCURSOR) is 

    begin 
     open recs_Table for 
      select * from table1, table2; 
    end; 

这上面的程序将被用于打开游标

create or replace procedure pro_sample(recs_Table out SYS_REFCURSOR) is 
    sam sys_refcursor; 
    var number; 
       -- if you have any variables then declare them 
    begin 
    pro_sample(sam); 
    fetch sam into var; 
    if sam%found then 
     --do this 
     else 
     --do that 
    end if; 
    close sam; 
end; 

上述程序将帮助你了解游标是否包含行或不是

0
create or replace procedure pro_sample(recs_Table out SYS_REFCURSOR) is 
    begin 
     open recs_Table for 
      select a,b,c,d from table1, table2; 
    end; 

create or replace function haveRows_pro_sample is 
sam sys_refcursor; 
var varchar(200); 
varOut number:=0; 
    begin 
     pro_sample(sam); 
     fetch sam into var,var,var,var; 
     if sam%found then 
      varOut :=1; 
     end if; 
     return varOut; 
    end; 
0

这工作对我来说:D

IF(MYCURSOR%ROWCOUNT = 0)THEN 
     DO SOMETHING ... 
    ENDIF; 
+0

只需解释一下你的代码 –

相关问题