Oracle PL/SQL-为存储在变量中的循环传递查询
我有一个场景,我需要用一些if/else语句创建一个动态查询,我已经准备好了查询,但无法使用下面这个内部循环,这是我试图实现的代码片段 查询:Oracle PL/SQL-为存储在变量中的循环传递查询,oracle,plsql,Oracle,Plsql,我有一个场景,我需要用一些if/else语句创建一个动态查询,我已经准备好了查询,但无法使用下面这个内部循环,这是我试图实现的代码片段 查询: select user_name into username from table1 where user ='user1'; for(query) Loop Dbms_output.put_line('user name ' || user_name); END Loop; 这可以在for循环中使用vaiable吗?不太可能。公
select user_name into username from table1 where user ='user1';
for(query)
Loop
Dbms_output.put_line('user name ' || user_name);
END Loop;
这可以在for循环中使用vaiable吗?不太可能。公告中写道:
光标
输入循环游标时未打开的显式游标(不是游标变量)的名称
您必须通过显式地将光标提取到记录中,直到您点击cursorname%notfound
,来对其进行编码
create or replace procedure cheese_report
( cheese_cur in sys_refcursor )
as
type cheese_detail is record
( name varchar2(15)
, region varchar2(30) );
cheese cheese_detail;
begin
loop
fetch cheese_cur into cheese;
dbms_output.put_line(cheese.name || ', ' || cheese.region);
exit when cheese_cur%notfound;
end loop;
close cheese_cur;
end;
测试:
在Oracle 21c中,您可以稍微简化这一点,尽管您仍然需要了解结果集的结构:
create or replace procedure cheese_report
( cheese_cur in sys_refcursor )
as
type cheese_detail is record
( cheese varchar2(15)
, region varchar2(30) );
begin
for r cheese_detail in values of cheese_cur
loop
dbms_output.put_line(r.cheese || ', ' || r.region);
end loop;
end;
您可以使用dbms\u sql
解析未知的ref游标来查找列名和类型,但这并不简单,因为您必须自己完成每个处理步骤。有关类似内容的示例,请参见
create or replace procedure cheese_report
( cheese_cur in sys_refcursor )
as
type cheese_detail is record
( cheese varchar2(15)
, region varchar2(30) );
begin
for r cheese_detail in values of cheese_cur
loop
dbms_output.put_line(r.cheese || ', ' || r.region);
end loop;
end;