Question

I am engaged at the moment in some serious PL/SQL programming. The format for creating procedures is

[CREATE [OR REPLACE]]
   PROCEDURE procedure_name[(parameter[, parameter]...)]
[AUTHID {DEFINER | CURRENT_USER}] {IS | AS}
   [PRAGMA AUTONOMOUS_TRANSACTION;]
   [local declarations]
BEGIN
   executable statements
[EXCEPTION
   exception handlers]
END [name];

and I place executable statements under BEGIN. Why am I not allowed to place a CREATE TABLE statement as an executable statement within a procedure?

Was it helpful?

Solution

Because CREATE TABLE is a DDL statement, and you may not execute DDL statements from PL/SQL (at least not directly).

If (and that's a big if) you really need to do that, you can either use the DBMS_SQL package or EXECUTE IMMEDIATE (easier):

create or replace procedure do_it as 
begin
  execute immediate 'CREATE TABLE foo(pk number not null)';
end;

But this is usually not necessary. People coming from a SQL Server background often are trained to create lots of temporary tables from their stored procedures - in Oracle, you'd normally use a Global Temporary Table for that.

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