Skip to Main Content
  • Questions
  • Running Select statement from tables column

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Jori.

Asked: June 17, 2016 - 10:56 am UTC

Last updated: June 18, 2016 - 6:02 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hi
Is it possible to store select statement into table’s column and then run that question?
Example
Insert into table1
(column1) values (select sysdate from dual))
And after that runt that specific question ("select sysdate from dual")?

and Connor said...

Yes, it is easy .... and very very dangerous.

For example:

SQL> create table t (stmt varchar2(1000) );

Table created.

SQL>
SQL> insert into t values ('select sysdate from dual');

1 row created.

SQL>
SQL> set serverout on
SQL> declare
  2    x date;
  3  begin
  4    for i in ( select * from t )   loop
  5      execute immediate i.stmt into x;
  6      dbms_output.put_line(x);
  7    end loop;
  8  end;
  9  /
18-JUN-16

PL/SQL procedure successfully completed.


Now of course, this assuming that every statement returns a single column, namely a date. If you want to be generic, then things get a little more complicated, eg


SQL>
SQL> drop table t purge;

Table dropped.

SQL> create table t (stmt varchar2(1000) );

Table created.

SQL>
SQL> insert into t values ('select sysdate from dual');

1 row created.

SQL> insert into t values ('select deptno, empno from scott.emp order by 1');

1 row created.

SQL>
SQL>
SQL> set verify off
SQL> declare
  2      l_theCursor     integer default dbms_sql.open_cursor;
  3      l_columnValue   varchar2(4000);
  4      l_status        integer;
  5      l_descTbl       dbms_sql.desc_tab;
  6      l_colCnt        number;
  7      n number := 0;
  8    procedure p(msg varchar2) is
  9      l varchar2(4000) := msg;
 10    begin
 11      while length(l) > 0 loop
 12        dbms_output.put_line(substr(l,1,80));
 13        l := substr(l,81);
 14      end loop;
 15    end;
 16  begin
 17    for q in ( select * from t )
 18    loop
 19      dbms_output.put_line( '--------------------------------------' );
 20      dbms_output.put_line( q.stmt );
 21
 22      dbms_sql.parse(  l_theCursor,  q.stmt, dbms_sql.native );
 23      dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );
 24
 25      for i in 1 .. l_colCnt loop
 26          dbms_sql.define_column(l_theCursor, i, l_columnValue, 4000);
 27      end loop;
 28
 29      l_status := dbms_sql.execute(l_theCursor);
 30
 31      while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
 32          for i in 1 .. l_colCnt loop
 33              dbms_sql.column_value( l_theCursor, i, l_columnValue );
 34              p( rpad( l_descTbl(i).col_name, 30 )
 35                || ': ' ||
 36                l_columnValue );
 37          end loop;
 38          dbms_output.put_line( '-----------------' );
 39          n := n + 1;
 40      end loop;
 41      if n = 0 then
 42        dbms_output.put_line( chr(10)||'No data found '||chr(10) );
 43      end if;
 44    end loop;
 45  end;
 46  /
--------------------------------------
select sysdate from dual
SYSDATE                       : 18-JUN-16
-----------------
--------------------------------------
select deptno, empno from scott.emp order by 1
DEPTNO                        : 10
EMPNO                         : 7782
-----------------
DEPTNO                        : 10
EMPNO                         : 7839
-----------------
DEPTNO                        : 10
EMPNO                         : 7934
-----------------
DEPTNO                        : 20
EMPNO                         : 7566
-----------------
DEPTNO                        : 20
EMPNO                         : 7902
-----------------
DEPTNO                        : 20
EMPNO                         : 7876
-----------------
DEPTNO                        : 20
EMPNO                         : 7369
-----------------
DEPTNO                        : 20
EMPNO                         : 7788
-----------------
DEPTNO                        : 30
EMPNO                         : 7521
-----------------
DEPTNO                        : 30
EMPNO                         : 7844
-----------------
DEPTNO                        : 30
EMPNO                         : 7499
-----------------
DEPTNO                        : 30
EMPNO                         : 7900
-----------------
DEPTNO                        : 30
EMPNO                         : 7698
-----------------
DEPTNO                        : 30
EMPNO                         : 7654
-----------------

PL/SQL procedure successfully completed.

SQL>


So why is this so dangerous ... well....what happens when someone inserts

"drop table t"

or

"grant dba to me"

etc etc..

Anything which lets SQL be dynamically constructed or run opens up a lot of issues to be careful of.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, June 20, 2016 - 5:17 am UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library