For 循环选择列表

问题描述 投票:0回答:2

我正在尝试使用 PL/SQL 执行以下操作:

  1. 使用查询获取字符串列表
  2. 使用 for 循环以列表作为输入进行查询。

到目前为止我有这个:

DECLARE 
sub2 varchar2(12);

cursor sub is Select ID_SUBLIN from TABLE 1 group by ID_SUBLIN;

BEGIN
for sub2 in sub LOOP
for inner in (select * from TABLE2 where PARAMETER=sub2.ID_SUBLIN )
loop
DBMS_OUTPUT.PUT_LINE( sub2 );
end loop;
end loop;
END;

但是它不起作用。我只是迭代 11 个项目列表

oracle for-loop stored-procedures plsql database-cursor
2个回答
5
投票

你有一些结构性问题和拼写错误。请尝试以下方法:

DECLARE 
-- sub2 varchar2(12);  
--> record_index "sub2" should exist in "for sub2 in sub", not as variable.
  cursor sub is Select ID_SUBLIN from TABLE1 group by ID_SUBLIN;
                                    --TABLE^1 [table name can't contain a space]   
BEGIN
for sub2 in sub 
loop
  for inner in (select * from TABLE2 where PARAMETER=sub2.ID_SUBLIN )
  loop
   dbms_output.put_line( sub2.ID_SUBLIN ); -- must be as <record_index>.<aColumn>
   dbms_output.put_line( inner.parameter );
-- by the way, both of the record_indexes with columns of cursors may be used here.
  end loop;
end loop;
END;
/

1
投票

另一种方式是在游标中组合 2 个查询,然后循环。

DECLARE
    sub2   VARCHAR2(12);
cursor sub is 
SELECT id_sublin
FROM TABLE1
JOIN table2 
ON table2.parameter = TABLE1.id_sublin 
group    by    id_sublin;

BEGIN
    FOR sub2 IN sub 
    LOOP
        dbms_output.put_line(sub2.id_sublin);
        EXIT WHEN sub%notfound;
    END LOOP;
END;
© www.soinside.com 2019 - 2024. All rights reserved.