pl / sql块代码问题,返回多个记录

时间:2018-02-07 20:52:03

标签: oracle plsql

无法完成这项工作......有多个表名,我需要将其传递给select。每个选择将返回多个记录。应在屏幕上将结果集打印给用户。

SQL> set serveroutput on;
SQL> DECLARE
    vs_statement VARCHAR2 (1000);
    my_var1      VARCHAR2(100);
    my_var2      VARCHAR2(100);
    CURSOR c1 IS
      SELECT table_name
      FROM   all_tables
      WHERE  table_name LIKE Upper('redit_1%');
BEGIN
    FOR table_rec IN c1 LOOP
        vs_statement :=
        'select a.userinfo, a.userstatus into my_var1, my_var12 from '
        || table_rec.table_name
        || ' A, FILES b  where A.objectid = B.id order by 1';

        EXECUTE IMMEDIATE vs_statement INTO my_var1,
                                            my_var2;

        dbms_output.Put_line(my_var1
                             ||'     '
                             || my_var2);
    END LOOP;
END;
/  

2 个答案:

答案 0 :(得分:1)

此行应如下所示(即删除INTO子句):

vs_statement :=
    'select a.userinfo, a.userstatus from '
    || table_rec.table_name
    || ' A, FILES b  where A.objectid = B.id order by 1';

另外,请确保此SELECT仅返回1行,否则最终会出现TOO-MANY-ROWS错误。

除此之外,我想它应该有用。

[编辑,关于对TOO-MANY-ROWS的恐惧]

嗯,我创建了一个全新的PL / SQL BEGIN-END块,其中包含一个可以完成工作的循环,显示SELECT语句返回的所有行。

由于我没有你的桌子,我使用了HR。看看:

SQL> DECLARE
  2      vs_statement VARCHAR2 (1000);
  3      my_var1      VARCHAR2(100);
  4      my_var2      VARCHAR2(100);
  5      CURSOR c1 IS
  6        SELECT table_name
  7        FROM   all_tables
  8        WHERE  table_name LIKE Upper('%departments%');
  9  BEGIN
 10      FOR table_rec IN c1 LOOP
 11          vs_statement := 'begin for cur_r in ( '
 12                          || Chr(10)
 13                          || 'select distinct a.department_id, a.department_name from '
 14                          || Chr(10)
 15                          || table_rec.table_name
 16                          ||
 17                              ' A, employees b  where A.department_id = B.department_id order by 1) loop'
 18                          || Chr(10)
 19                          || ' dbms_output.put_line(cur_r.department_name); '
 20                          || Chr(10)
 21                          || ' end loop; end;';
 22
 23          EXECUTE IMMEDIATE vs_statement; -- into my_var1, my_var2;
 24      END LOOP;
 25  END;
 26  /
Administration
Marketing
Purchasing
Human Resources
Shipping
IT
Public Relations
Sales
Executive
Finance
Accounting

PL/SQL procedure successfully completed.

SQL>

答案 1 :(得分:0)

  

“每个选择将返回多个记录...应在屏幕上向用户打印结果集”

为每个生成的语句打开引用游标。循环并显示值。

DECLARE
    rc sys_refcursor;
    vs_statement VARCHAR2 (1000);
    my_var1      VARCHAR2(100);
    my_var2      VARCHAR2(100);
    CURSOR c1 IS
      SELECT table_name
      FROM   all_tables
      WHERE  table_name LIKE Upper('redit_1%');
BEGIN
    << tab_loop >>
    FOR table_rec IN c1 LOOP
        vs_statement :=
        'select a.userinfo, a.userstatus from '
        || table_rec.table_name
        || ' A, FILES b  where A.objectid = B.id order by 1';

        open rc for vs_statement;
        dbms_output.put_line('records for '||table_rec.table_name);
        << rec_loop >>
        loop
            fetch rc into my_var1,my_var2;
            exit when rc%notfound;
            dbms_output.Put_line(my_var1
                             ||'     '
                             || my_var2);
        end loop rec_loop;
        close rc;

    END LOOP tab_loop;
END;
/