質問

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:

SELECT tablename_dml('UPDATE tablename SET foo = ''bar''') FROM dual;

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:

EXECUTE IMMEDIATE 'BEGIN'||query||' RETURNING col1, col2 BULK COLLECT INTO :1, :2;END;' USING OUT col1_t, col2_t;

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.

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:

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;

The last statement produces the following error: (the SELECT that is mentioned there is the SELECT min(id))

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

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.

Unfortunately, I'm stuck with 8i and removing BEGIN .. END is not an option (see the explanation above).

Is there a specific Oracle 8i limitation in play here? Is it possible to overcome it with dynamic SQL?

役に立ちましたか?

解決

Not sure why you need to do all this work. Oracle 8i supported RETURNING INTO with bulk collection. Find out more

So you should just be able to execute this statement in non-dynamic SQL. Something like this:

UPDATE tablename 
SET foo = 'bar' 
returning  col1, col2 bulk collect into col1_t, col2_t;

他のヒント

Stripped of all the irrelevancies, I think your question is simple.

This update statement runs in SQL:

update xy set id = id + (SELECT min(id) FROM xy);

And this anonymous block also runs:

begin
    update xy set id = id + 100;
end;

But combining the two doesn't work:

begin
    update xy set id = id + (SELECT min(id) FROM xy);
end;

Probably you have run into a limitation of older Oracle. Prior to 9i, the SQL engine and the PL/SQL SQL engine were always out of sync. So latest features supported in SQL often weren't supported in PL/SQL. It seems like you have one of those.

Since 9i Oracle have striven to keep the two engines in sync, so it is much rarer to find things which work in SQL but not in PL/SQL.

Given the nature of your task, upgrading your version of Oracle is out. So all I can suggest is that you have two procedures, one which supports the sub query syntax (by avoiding the need for such subqueries. Something like this:

CREATE OR REPLACE FUNCTION xy_sqfn
    (main_query VARCHAR2
      , sub_query VARCHAR2   ) 
    RETURN NUMBER 
IS      
    n pls_integer;
BEGIN         
    execute immediate sub_query into n;
    EXECUTE IMMEDIATE 'BEGIN '||main_query||'; END;'
          using n;
    RETURN 5; 
END;

call it like this

result := xy_sqfn ('update xy set id = id + :1'
                   , 'SELECT min(id) FROM xy');

Now this approach won't work for correlated sub-queries. So it you have any of them, you'll need to do something different again.


Incidentally, using the AUTONOMOUS TRANSACTION pragma to fudge executing DML in a SELECT statement is quite horrible. Why not just run the functions in PL/SQL? Or use procedures? I suppose you'll say it doesn't matter because you're just writing some shonky code to support a data migration. Which is fair enough, but for the benefit of future seekers: don't do this! It's very bad practice!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top