Note that there are some explanatory texts on larger screens.

plurals
  1. POOracle 8i dynamic SQL error on subselects in pl/sql blocks
    primarykey
    data
    text
    <p>I wrote an Oracle function (for 8i) to fetch rows affected by a DML statement, emulating the behavior of RETURNING * from PostgreSQL. A typical function call looks like:</p> <p><code>SELECT tablename_dml('UPDATE tablename SET foo = ''bar''') FROM dual;</code></p> <p>The function is created automatically for each table and uses Dynamic SQL to execute a query passed as an argument. Moreover, a statement that executes the query dynamically is also wrapped in a BEGIN .. END block:</p> <p><code>EXECUTE IMMEDIATE 'BEGIN'||query||' RETURNING col1, col2 BULK COLLECT INTO :1, :2;END;' USING OUT col1_t, col2_t;</code></p> <p>The reason behind this perculiar construction is that it seems to be the only way to get values from the DML statement that affects multiple rows. Both col1_t and col2_t are declared as collections of the types corresponding to the table columns.</p> <p>Finally, to the problem. When the query passed contains a subselect, execution of the function produces a syntax error. Below is a simpe example to illustrate this:</p> <pre><code>CREATE TABLE xy(id number, name varchar2(80)); CREATE OR REPLACE FUNCTION xy_fn(query VARCHAR2) RETURN NUMBER IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN EXECUTE IMMEDIATE 'BEGIN '||query||'; END;'; ROLLBACK; RETURN 5; END; SELECT xy_fn('update xy set id = id + (SELECT min(id) FROM xy)') FROM DUAL; </code></pre> <p>The last statement produces the following error: (the SELECT that is mentioned there is the SELECT min(id))</p> <blockquote> <p>ORA-06550: line 1, column 32: PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + mod not null others avg count current exists max min prior sql stddev sum variance execute forall time timestamp interval date </p> </blockquote> <p>This problem occurs on 8i (8.1.6), but not 10g. If BEGIN .. END blocks are removed - the problem disappears. If a subselect in a query is replaced with something else, i.e. a constant, the problem disappears.</p> <p>Unfortunately, I'm stuck with 8i and removing BEGIN .. END is not an option (see the explanation above).</p> <p>Is there a specific Oracle 8i limitation in play here? Is it possible to overcome it with dynamic SQL?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload