Skip to Main Content
  • Questions
  • Are we really create table aumatically as per our requirement

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Abhijit.

Asked: August 05, 2016 - 10:56 am UTC

Last updated: August 07, 2016 - 4:47 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi,


I want to create a table automatically as per based of various column data types and various number of column.

Please ! dont cconsider the constraint.

for example:

table name :table1

required output is :

DDl structure of created table table1 is,
-----------------------
ID(int) | name varchar
------------------------
Next time requirement will be change means number of column and its datatype will be change

ok,I will try to explain my quiz
suppose i want to create a table employee and its column name is eno int,fname varchar,lname varchar

suppose i wan create another table dept and its column name dno int ,dname varchar

If i wana create above table automatically So what can I do for that .
Is this possible?Is it hve any solution for that?






and Connor said...

Sorry - I dont know what you mean. Do you mean someone passes you the column names and data types and you run a create table command ?

In that case, it would just be a plsql routine - a basic example below, but you could extend it

SQL> create or replace
  2  procedure GEN_TAB(p_tname varchar2, p_cols sys.odcivarchar2list, p_datatypes sys.odcivarchar2list) is
  3    ddl varchar2(32767);
  4  begin
  5    ddl := 'create table '||p_tname||'(';
  6    for i in 1 .. p_cols.count loop
  7      ddl := ddl || p_cols(i)||' '||p_datatypes(i)||',';
  8    end loop;
  9    ddl := rtrim(ddl,',')|| ')';
 10
 11    dbms_output.put_line(ddl);
 12  end;
 13  /

Procedure created.

SQL>
SQL>
SQL> set serverout on
SQL> exec gen_tab('my_tab',sys.odcivarchar2list('a','b','c'),sys.odcivarchar2list('int','date','int'));
create table my_tab(a int,b date,c int)

PL/SQL procedure successfully completed.



Is this answer out of date? If it is, please let us know via a Comment

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library