To create a table inside a PL/SQL block, you can use the EXECUTE IMMEDIATE
statement, which allows you to execute a dynamic SQL or PL/SQL statement. Here is an example of how you can use this statement to create a table:
BEGIN -- Define the SQL statement to create the table EXECUTE IMMEDIATE 'CREATE TABLE my_table (id NUMBER PRIMARY KEY, name VARCHAR2(50))'; END;
In this example, the EXECUTE IMMEDIATE
statement is used to execute the dynamic SQL statement 'CREATE TABLE my_table (id NUMBER PRIMARY KEY, name VARCHAR2(50))'
, which creates a table called my_table
with two columns: id
and name
.
You can also use the EXECUTE IMMEDIATE
statement to insert data into the table, like this:
BEGIN -- Define the SQL statement to insert data into the table EXECUTE IMMEDIATE 'INSERT INTO my_table (id, name) VALUES (1, ''John Doe'')'; END;
This example uses the EXECUTE IMMEDIATE
statement to execute the dynamic SQL statement 'INSERT INTO my_table (id, name) VALUES (1, ''John Doe'')'
, which inserts a new row into the my_table
table with the values 1
and 'John Doe'
for the id
and name
columns, respectively.