display select results inside anonymous block

橙三吉。 提交于 2019-12-04 03:11:55

You can try with this, to print your result easily:

declare
your_variable varchar2(19);
BEGIN
DBMS_OUTPUT.PUT_LINE('init..');
 FOR x IN (SELECT      your_column
                 FROM you_table
                 where rownum<2
             order by 1)
   LOOP
      DBMS_OUTPUT.PUT_LINE(x.your_column);
   END LOOP;
END;

Error

For what I know, whatever tool you use to debug, the pl/sql blocks(anonymous and named) should be valid for the PL/SQL compiler. The fact is that your block doesn't result valid for the PL/SQL compiler, and your error is there to tell you, and is coming out from the PL/SQL compiler and not from the Sql Developer!

PLS-00428: an INTO clause is expected in this SELECT statement Cause: The INTO clause of a SELECT INTO statement was omitted. For example, the code might look like SELECT deptno, dname, loc FROM dept WHERE ... instead of SELECT deptno, dname, loc INTO dept_rec FROM dept WHERE ... In PL/SQL, only a subquery is written without an INTO clause. Action: Add the required INTO clause

and

ORA-06550: line string, column string: string Cause: Usually a PL/SQL compilation error. Action: None

Why error

When an Pl/sql error appear, you only have the choice to investigate in the code and in the manuals: Resolution of names in static SQL statements


PS: The route is always the same:

How to ask

All the "oracles" are here:

Sql Developer

Cos Callis

In order to return the value of the select it needs to be selected into a container (a reference cursor or REF CURSOR). In your Declare you should include ref_cursor_out SYS_REFCURSOR; and change your select to:

select * into ref_cursor_out ...

In SQL Developer there is an option (I am a Toad user, so I forget where in SD) that tells the IDE to load the result set into a grid to view.

[edit: per comment from @DCookie, Thanks for the catch!]

I recently changed from MSSQL to PLSQL and I miss the return values as tables from procedures for analytical purposes. I wrote simply dynamic query that return table by two steps. Maybe someone use it:

/* 
rkry20150929: Return table from anonymous block 
*/
declare
v_stmt varchar2(1000);
c int;
BEGIN
 select count(*) into c from user_tables where table_name = upper('tmp_result');
 if c>0
 THEN 
  v_stmt := 'truncate table tmp_result';
  execute immediate v_stmt; 
  v_stmt := 'drop table tmp_result';
  execute immediate v_stmt; 
 end if;
 v_stmt :='CREATE GLOBAL TEMPORARY TABLE tmp_result on commit preserve rows AS ';
 v_stmt:= v_stmt || 
 /*-----THERE FILL SQL COMMAND-----------*/'
 SELECT ''Result select to table in anonymous block '' MyColumn FROM DUAL
 ';/*-----THERE FILL SQL COMMAND-----------*/
 execute immediate v_stmt;
 End;
 /*FIRST EXECUTE TO HERE */
SELECT * FROM tmp_result;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!