How to view the content of a resultset in Toad from a stored procedure with unknown number of columns?

删除回忆录丶 提交于 2020-01-06 20:01:05

问题


In Tsql I can execute a stored procedure in Query Analyzer and view the content of a resultset right there query analyzer window without know anything about the query structure (tables, columns, ...)

--Tsql sample exec myproc parm1, parm2, parm3

Now I am working with PLsql and Toad (which I am relatively new at for Toad). I need to view the content of a resultset of a convoluted stored procedure, and I don't know what the number of columns is -- let alone their data types (this proc is composed of several freaky subqueries -- which I can view individually, but they get pivoted, and the number of columns varies in the final resultset). How can I view the content of this resultset in Toad when I execute the procedure when I don't know how many columns there are or their data types?

Below is code that I have mustered together for viewing the content of a result set of stored procedures where I know how many columns there are and their data types ahead of time. In my code sample below I use a sys_refcursor that I named x_out and I also create a temporary table to store the content of the resultset for additional viewing. Is there a way I can do this when I don't know how many columns there are in the resultset? How to do this with PLsql -- Toad?

create global temporary table tmpResult (fld1 number, fld2 varchar(50), fld3 date);

declare
  x_out sys_refcursor;  
  tmpfld1 number;
  tmpfld2 varchar2(50);
  tmpfld3 date;

BEGIN
  myschema.mypkg.myproc(parm1, parm2, x_out);

LOOP
    FETCH x_out INTO tmpfld1, tmpfld2, tmpfld3;
    DBMS_OUTPUT.Put_Line ('fld1:-- '||tmpfld1||': fld2:-- '||tmpfld2||':   fld3:-- '||tmpfld3);

-- I also insert the result set to a temp table for additional viewing of the data from the stored procedure

    Insert Into tmpResult values(tmpfld1, tmpfld2, tmpfld3);        
    EXIT WHEN x_out%NOTFOUND;    

END LOOP;

END;

回答1:


Toad can automatically retrieve the cursor for you. You have a few options, #3 perhaps is the easiest if you just want to see the data.

  1. If you have the myschema.mypkg loaded in the Editor you can hit F11 to execute it. In the dialog that shows select your package member to the left and select the Output Options tab. Check the option to fetch cursor results or use the DBMS Output options. Click OK and the package executes. Depending on your Toad version you'll see a grid at the bottom of Editor for your results or you'll see a PL/SQL results tab. If you see the latter double click the (CURSOR) value in the output column for your output argument. I suggest using the fetch option as long as your dataset isn't so large that it will cause Out of Memory errors.

  2. Locate your package in the Schema Browser and rt-click, Execute Package. You'll see the same dialog as mentioned in #1. Follow the remaining steps there.

  3. Use a bind variable from an anonymous block. Using your example you'd want something like this...

    declare x_out sys_refcursor; begin myschema.mypkg.myproc(parm1, parm2, x_out); :retval := x_out; end;

Execute this with F9 in the Editor. In the Bind Variable popup set the datatype of retval to Cursor. Click OK. Your results are then shown in the data grid. Again if your dataset is very large you may run out of memory here.




回答2:


StackOverflow not letting me post this other solution:

I try posting part of this other solution (if SOF lets me) - this the 2nd half of the other way:

BEGIN 
myschema.mypkg.myproc(parm1, parm2, parm3 x_out); 

FOR rec_ IN get_columns LOOP 
  DBMS_OUTPUT.put_line(rec_.name || ': ' || rec_.VALUE); 
END LOOP;   
END;



回答3:


and here is the 1st half of the other way:

DECLARE 
x_out SYS_REFCURSOR; 

CURSOR get_columns IS 

...



来源:https://stackoverflow.com/questions/35658248/how-to-view-the-content-of-a-resultset-in-toad-from-a-stored-procedure-with-unkn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!