Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Wednesday 29 August 2018

PLSQL code to create a table dynamically at runtime

In this tutorial i will show you the PLSQL code to create a table dynamically at run time 



DECLARE
SQL_STMT VARCHAR2(500);
VAL1 VARCHAR2(50) := 'DYNAMIC_COL1 NUMBER';
VAL2 VARCHAR2(50) := 'COLUMN_2 VARCHAR2(100)'; ---column with their datatypes
tabname varchar2(100) := 'Auto_Created';      ---table name 
BEGIN  
SQL_STMT := 'CREATE TABLE '
||TabName||
'('
||val1||','
||VAL2||
/*.......Any number of columns................*/
')';
EXECUTE IMMEDIATE SQL_STMT ;
END;  


In this code,

we provide the desired table name which is stored in variable 'tabname'
we store the column names with their respective data types in variables of character type.

EXECUTE IMMEDIATE clause is used to execute the query at run time.




No comments:

Post a Comment