Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is static ddl not allowed in PL/SQL?

Tags:

oracle

plsql

In an Oracle PL/SQL block, why is dynamic sql allowed

begin
    execute immediate 'drop table table_name';
end;

but static is not?

begin
    drop table table_name;
end;

I hope the answer is more insightful than "because that's how the language works".

like image 593
Alex Avatar asked Mar 05 '13 19:03

Alex


People also ask

Can static SQL be used in Plsql?

Static SQL is a PL/SQL feature that allows SQL syntax directly in a PL/SQL statement.

Can we write DDL statements in PL SQL?

You can execute a DDL through PL/SQL program either by using DBMS_SQL package or by using the Execute Immediate statement of Native Dynamic SQL. The latter option is the most used one, specially now a days because of its better performance and easy to learn syntax.

Why we Cannot use DDL commands in procedure?

You can use only DDL COMMENT statements in a stored procedure. You cannot specify DML COMMENT statements, which are restricted to embedded SQL applications, to fetch the comments for database objects, columns of a table, and parameters.

Which SQL statements Cannot be used directly in PL SQL?

You want to execute a SQL data definition statement (such as CREATE ), a data control statement (such as GRANT ), or a session control statement (such as ALTER SESSION ). Unlike INSERT , UPDATE , and DELETE statements, these statements cannot be included directly in a PL/SQL program.


2 Answers

The answer is PL/SQL does not support dynamic polymorphism. it only supports static polymorphism because

All PL/SQL generats a "DIANA" -> Descriptive Intermediate Attributed Notation for Ada , a tree-structured intermediate language. DIANA is used internally by compilers.

At compile time, PL/SQL source code is translated into system code and generates corresponding DIANA. Now think if there were a DDL statement like create table statement which at the compile time does not exists it will be created after running the program. how would your PL/SQL engine generate a DIANA then ????

The DIANA is plays an important role in PL/SQL to check/validate that the sub program. this is required because as we know that a sub-program can use database objects such as Tables,Views,Synonyms or other stored procs. it could be possible that the the objects may have changed/removed/droped when next time you run the program. For ex : some one might have droped the table, the stored proc or function singnature may have changed.

Thats why generally PL/SQL is used to manipulate the data within database structure, but not to manipulate those structures.

but there are ways to manipulate using dynamic SQL and DBMS_SQL package but theses methodlogy are again should be used cautiously. For example if you are creating a Table you should check first if this table is already exists or not using data dictionary views.

like image 200
Gourabp Avatar answered Oct 05 '22 08:10

Gourabp


Probably because otherwise some code would be like:

  begin
    create table tmp (n number);
    insert into tmp values (1);
  end;

And we would expect the compiler to know that at time of the insert, the table exists. The compilation of the block would me much more difficult. Here it is a very simple case, but we can easily imagine some conditional branching, and complex blabla.

But, since we need to put the DDL in an execute immediate block, the limitation maybe somehow easier to understand.

Just an idea...

like image 20
Plouf Avatar answered Oct 05 '22 09:10

Plouf