Question

Is it possible to execute some sql commands within one EXECUTE IMMEDIATE block?

What is wrong with this syntax:

declare
    pragma autonomous_transaction;
begin           
    execute immediate
    'begin
        COMMENT ON TABLE t1 IS ''description1'';
        COMMENT ON TABLE t2 IS ''description2'';            
    end;';
end;

For one SQL command it works fine:

declare
    pragma autonomous_transaction;
begin           
    execute immediate ' COMMENT ON TABLE t1 IS ''description1'' ';
end;
Was it helpful?

Solution

The begin end within the string to execute immediate is going to be treated as a PL/SQL anonymous block. DDL, such as COMMENT is not allowed in PL/SQL. If it were you wouldn't need to use execute immediate. Oracle essentially works with either a block of PL/SQL statement or a single SQL statement at a time. Though there are APIs to batch SQL statements too.

So, to run COMMENT within a PL/SQL block or procedure, you will need to execute immediate statements.

Without more context I can not intelligently comment on whether that is the right approach, or if just having the two comment statements stand alone would be better.

OTHER TIPS

Well, you could do this:

begin           
    execute immediate
    'begin
        execute immediate ''COMMENT ON TABLE t1 IS ''''description1'''' '';
        execute immediate ''COMMENT ON TABLE t2 IS ''''description2'''' '';
    end;';
end;

But there's not much point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top