Hél*_*ves 3 sql oracle plsql 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;
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用,有什么帮助吗?Ty
在使用 %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;
Run Code Online (Sandbox Code Playgroud)
请注意,您可能需要向 FETCH 语句的 INTO 子句添加变量的方式,为 TABLE1 和 TABLE2 中的每一列添加一个变量。另请注意,此游标的写入方式可能会返回比预期更多的行;因为没有指定连接条件,您将获得所谓的笛卡尔连接,其中 TABLE1 中的每一行都连接到 TABLE2 中的每一行 - 因此,您将返回的行数是 (# of rows in 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;
Run Code Online (Sandbox Code Playgroud)
分享和享受。
| 归档时间: |
|
| 查看次数: |
27544 次 |
| 最近记录: |