Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: DBMS_UTILITY.EXEC_DDL_STATEMENT vs EXECUTE IMMEDIATE

Tags:

Which are the differences between DBMS_UTILITY.EXEC_DDL_STATEMENT and EXECUTE IMMEDIATE?

like image 312
Revious Avatar asked Aug 07 '11 13:08

Revious


People also ask

Is execute immediate faster?

Native dynamic SQL using the EXECUTE IMMEDIATE and OPEN-FOR statements is faster and requires less coding than the DBMS_SQL package.

What is Oracle execute immediate?

The EXECUTE IMMEDIATE statement executes a dynamic SQL statement or anonymous PL/SQL block. You can use it to issue SQL statements that cannot be represented directly in PL/SQL, or to build up statements where you do not know all the table names, WHERE clauses, and so on in advance.

Can we use execute immediate in forall?

You can put an EXECUTE IMMEDIATE statement with the RETURNING BULK COLLECT INTO inside a FORALL statement. You can store the results of all the INSERT , UPDATE , or DELETE statements in a set of collections. You can pass subscripted collection elements to the EXECUTE IMMEDIATE statement through the USING clause.

How do you execute a procedure using execute immediate in Oracle?

Notice the USING clause in the EXECUTE IMMEDIATE to bind the values to the bind variables in the string. Try this, execute immediate ('begin '||oprocstring||' end;'); In other words, wrap the string with a begin/end and it should work.


1 Answers

Fundamentally they do the same thing, which is to provide a mechanism to execute DDL statements in PL/SQL, which isn't supported natively. If memory serves me well, the EXEC_DDL_STATEMENT was available in the Oracle 7 version of the DBMS_UTILITY package, whereas Native Dynamic SQL (EXECUTE IMMEDIATE) was only introduced in 8.

There are a couple of differences. EXECUTE IMMEDIATE is mainly about executing dynamic SQL (as its NDS alias indicates). the fact that we can use it for DDL is by-the-by. Whereas EXEC_DDL_STATEMENT() - as the suggests - can only execute DDL.

But the DBMS_UTILITY version isn't retained just for backwards compatibility, it has one neat trick we cannot do with EXECUTE IMMEDIATE - running DDL in a distributed fashion. We can run this statement from our local database to create a table on a remote database (providing our user has the necessary privileges there):

SQL>  exec DBMS_UTILITY.EXEC_DDL_STATEMENT@remote_db('create table t1 (id number)'); 

I'm not recommending this, just saying it can be done.

like image 108
APC Avatar answered Oct 02 '22 16:10

APC