.ora-code.com

Links
Home
Oracle DBA Forum
Frequent Oracle Errors
TNS:could not resolve the connect identifier specified
Backtrace message unwound by exceptions
invalid identifier
PL/SQL compilation error
internal error
missing expression
table or view does not exist
end-of-file on communication channel
TNS:listener unknown in connect descriptor
insufficient privileges
PL/SQL: numeric or value error string
TNS:protocol adapter error
ORACLE not available
target host or object does not exist
invalid number
unable to allocate string bytes of shared memory
resource busy and acquire with NOWAIT specified
error occurred at recursive SQL level string
ORACLE initialization or shutdown in progress
archiver error. Connect internal only, until freed
snapshot too old
unable to extend temp segment by string in tablespace
Credential retrieval failed
missing or invalid option
invalid username/password; logon denied
unable to create INITIAL extent for segment
out of process memory when trying to allocate string bytes
shared memory realm does not exist
cannot insert NULL
TNS:unable to connect to destination
remote database not found'>ora-02019
exception encountered: core dump
inconsistent datatypes
no data found
TNS:operation timed out
PL/SQL: could not find program
existing state of packages has been discarded
maximum number of processes exceeded
error signaled in parallel query server
ORACLE instance terminated. Disconnection forced
TNS:packet writer failure
see ORA-12699
missing right parenthesis
name is already used by an existing object
cannot identify/lock data file
invalid file operation
quoted string not properly terminated
Dynamic SQL

Dynamic SQL

2006-04-06       - By Nigel Thomas
Reply:     1     2     3     4     5     6     7     8     9     10  

To summarise and clarify previous advice, here's how to make the anonymous
block work. I've changed the table and column names to work with tables in the
HR schema that comes with SQL*Developer (or was it JDeveloper...) but you can
easily change it to fit your requirements once you've understood the concepts

Regards Nigel
-- ---- ---- ---- ---- -----

DECLARE
m_output VARCHAR2(4000);
TYPE curtype IS REF CURSOR;
m_cursor curtype;
BEGIN
 -- ensure output is enabled
 -- you could use SQL*Plus "set serverout on size 1000000" instead
 DBMS_OUTPUT.ENABLE(100000);
 DBMS_OUTPUT.PUT_LINE('Start:');
 
 FOR r IN
   (SELECT owner
    FROM all_tables
    WHERE TABLE_NAME = UPPER('JOBS')
    ORDER BY owner)
 LOOP
   DBMS_OUTPUT.PUT_LINE('Owner = '||r.owner);

   -- I've put concatenation into the dynamic statement
   -- you could instead query any N columns here, fetching into N pl/sql
variables
   -- and then concatenate in the call to DBMS_OUTPUT.PUT_LINE below

   OPEN m_cursor FOR
      'SELECT JOB_ID||'':''||JOB_TITLE||'':''||MAX_SALARY FROM ' || r.owner ||
'.JOBS where min_salary = 4000';
   LOOP
     FETCH m_cursor
     INTO m_output;
     EXIT WHEN m_cursor % NOTFOUND;
     -- dbms_output.put_line takes a single parameter
     DBMS_OUTPUT.PUT_LINE(m_output);
   END LOOP;
 END LOOP;
END;
/

anonymous block completed
Start:
Owner = HR
IT_PROG:Programmer:10000
MK_REP:Marketing Representative:9000
HR_REP:Human Resources Representative:9000
<html><head><style type="text/css"><!-- DIV {margin:0px} --></style></head>
<body><div style="font-family:times new roman, new york, times, serif;font-size
:12pt"><DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york,
times, serif">
<DIV>To summarise and clarify previous advice, here's how to make the anonymous
block work. I've changed the table and column names to work with tables in&nbsp
;the HR schema that comes with SQL*Developer (or was it JDeveloper...) but you
can easily change it to fit your requirements once you've understood the
concepts</DIV>
<DIV>&nbsp;</DIV>
<DIV>Regards Nigel</DIV>
<DIV>-- ---- ---- ---- ---- -----</DIV>
<DIV>&nbsp;</DIV>
<DIV>DECLARE <BR>m_output VARCHAR2(4000);<BR>TYPE curtype IS REF CURSOR;<BR>m
_cursor curtype;<BR>BEGIN<BR>&nbsp; -- ensure output is enabled<BR>&nbsp; -- you
could use SQL*Plus "set serverout on size 1000000" instead<BR>&nbsp; DBMS
_OUTPUT.ENABLE(100000);<BR>&nbsp; DBMS_OUTPUT.PUT_LINE('Start:');<BR>&nbsp; <BR>
&nbsp; FOR r IN<BR>&nbsp;&nbsp;&nbsp; (SELECT owner<BR>&nbsp;&nbsp;&nbsp;&nbsp;
FROM all_tables<BR>&nbsp;&nbsp;&nbsp;&nbsp; WHERE TABLE_NAME = UPPER('JOBS')<BR
>&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY owner)<BR>&nbsp; LOOP<BR>&nbsp;&nbsp;&nbsp;
DBMS_OUTPUT.PUT_LINE('Owner = '||r.owner);</DIV>
<DIV><BR>&nbsp;&nbsp;&nbsp; -- I've put concatenation into the dynamic
statement<BR>&nbsp;&nbsp;&nbsp; -- you could instead query any N columns here,
fetching into N pl/sql variables<BR>&nbsp;&nbsp;&nbsp; -- and then concatenate
in the call to DBMS_OUTPUT.PUT_LINE below</DIV>
<DIV><BR>&nbsp;&nbsp;&nbsp; OPEN m_cursor FOR <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
;&nbsp; 'SELECT JOB_ID||'':''||JOB_TITLE||'':''||MAX_SALARY FROM ' || r.owner ||
'.JOBS where min_salary = 4000';<BR>&nbsp;&nbsp;&nbsp; LOOP<BR>&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; FETCH m_cursor<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INTO m
_output;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXIT WHEN m_cursor % NOTFOUND;</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- dbms_output.put_line takes a single
parameter<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DBMS_OUTPUT.PUT_LINE(m_output);<BR>
&nbsp;&nbsp;&nbsp; END LOOP;<BR>&nbsp; END LOOP;<BR>END;<BR>/</DIV>
<DIV>&nbsp;</DIV>
<DIV>anonymous block completed<BR>Start:<BR>Owner = HR<BR>IT_PROG:Programmer
:10000<BR>MK_REP:Marketing Representative:9000<BR>HR_REP:Human Resources
Representative:9000<BR><BR></DIV></DIV></div></body></html>