如何从匿名块返回表?

时间:2014-08-19 13:45:52

标签: sql stored-procedures plsql

我没有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:发现:how to print out the whole table using anonymous block in 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

2 个答案:

答案 0 :(得分:2)

我不是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;

答案 1 :(得分:1)

试试这个:

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;

我希望这会对你有所帮助。