繁体   English   中英

如何从匿名块返回表?

[英]How to return table from anonymous block?

我没有db的写权限,但我需要获得使用sql请求无法获取的结果。

所以我需要一个程序。

我可以声明一个匿名块来从控制台执行:

SQL> declare ... begin ... end;

我甚至可以在里面创建自定义表并将一些数据放入其中:

SQL> declare 
type RowType is record (column1 varchar(20), column2 integer);
type TableType is table of RowType;
resultTable TableType;
...
begin
...
execute immediate 'some dynamic request'
bulk collect into resultTable;
...
end;

但是如何查看“resultTable”中的内容?

UPD:发现这个: 如何在pl sql中使用匿名块打印整个表?

但是stil无法输出数据:

SQL> declare 
    type RowType is record (column1 varchar(20), column2 integer);
    type TableType is table of RowType;
    resultTable TableType;
    ...
    begin
    ...
    execute immediate 'some dynamic request'
    bulk collect into resultTable;
    ...
    FOR cursor1 IN (SELECT * FROM resultTable)                     --\
    LOOP                                                           --| this fails
      DBMS_OUTPUT.PUT_LINE('Column 1 = ' || cursor1.column1 ||     --|
                           ', Column 2 = ' || cursor1.column2);    --|
    END LOOP;                                                      --/
    end;

这导致ORA-00942: table or view does not exist

我不是100%确定这是你要问的问题,但你可以使用DBMS_OUTPUT.PUT_LINE从控制台查看resultTable的内容。

编辑 :我可能会诅咒语法,但我认为你可以这样做:

FOR indx IN 1 .. resultTable.COUNT 
LOOP
    DBMS_OUTPUT.PUT_LINE('Column 1 = ' || resultTable(indx).column1 || 
                         ', Column 2 = ' || resultTable(indx).column2);
END LOOP;

尝试这个:

declare 
type RowType is record (column1 varchar(20), column2 integer);
type TableType is table of RowType;
resultTable TableType;
...
begin
...
execute immediate 'some dynamic request'
bulk collect into resultTable;

FOR r IN 1..resultTable.count loop
    DBMS_OUTPUT.PUT_LINE(resultTable(r).column1 || ' ' ||resultTable(r).column2);
end loop;

我希望这可以帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM