GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  PL/SQL
Go To First  |  Previous Question  |  Next Question 
 PL/SQL  |  Question 156 of 241    Print  
Is it possible create table in procedure or function? If Not Why?

  
Total Answers and Comments: 3 Last Update: March 06, 2007     Asked by: ddkdhar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Nikhil_4_Oracle
 


Hi,

Its possible to create a table using procedures.
There are two ways,

 #1
      Use PRAGMA AUTONOMOUS_TRANSACTION;

 Code:


   Create or replace procedure Create_tab

    As

    PRAGMA AUTONOMOUS_TRANSACTION;

    Begin

     EXECUTE IMMEDIATE
    ‘Create TABLE ProcT(col1 number(2), col2 varchar2(10))’;

     End;



   #2

     Use Dbms_sql Package 

 Code:


 Create or Replace procedure Dynamic_Tab
 
(TabName in varchar2)

 as
 
V_cursor INTEGER;
Ddl_Execute INTEGER;

begin

V_cursor:=Dbms_sql.Open_cursor;

Dbms_sql.Parse(V_cursor,'Create Table  '||TabName||' (col1 number(2),col2 varchar2

(10))',Dbms_sql.native);

Ddl_Execute:=dbms_sql.Execute(V_cursor);

Dbms_output.put_line(TabName||' Created.');

If Dbms_sql.Is_open(V_cursor) then

Dbms_sql.Close_cursor(V_Cursor);

end if;

Exception

when Others then

Dbms_output.put_line(sqlerrm);

End Dynamic_Tab;

Regards,

Nikhil (iflex).



 

Above answer was rated as good by the following members:
ratna82, samareshp
March 05, 2007 07:28:12   #1  
Nikhil_4_Oracle        

RE: Is it possible create table in procedure or functi...


Hi

Its possible to create a table using procedures.
There are two ways

#1
Use PRAGMA AUTONOMOUS_TRANSACTION;

Code:


Create or replace procedure Create_tab

As

PRAGMA AUTONOMOUS_TRANSACTION;

Begin

EXECUTE IMMEDIATE
‘Create TABLE ProcT(col1 number(2) col2 varchar2(10))’;

End;



#2

Use Dbms_sql Package

Code:


Create or Replace procedure Dynamic_Tab

(TabName in varchar2)

as

V_cursor INTEGER;
Ddl_Execute INTEGER;

begin

V_cursor: Dbms_sql.Open_cursor;

Dbms_sql.Parse(V_cursor 'Create Table '||TabName||' (col1 number(2) col2 varchar2

(10))' Dbms_sql.native);

Ddl_Execute: dbms_sql.Execute(V_cursor);

Dbms_output.put_line(TabName||' Created.');

If Dbms_sql.Is_open(V_cursor) then

Dbms_sql.Close_cursor(V_Cursor);

end if;

Exception

when Others then

Dbms_output.put_line(sqlerrm);

End Dynamic_Tab;

Regards

Nikhil (iflex).




 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
March 05, 2007 22:00:00   #2  
ddkdhar Member Since: June 2006   Contribution: 48    

RE: Is it possible create table in procedure or functi...
thank you very mech nikhil
 
Is this answer useful? Yes | No
March 06, 2007 23:00:10   #3  
ddkdhar Member Since: June 2006   Contribution: 48    

RE: Is it possible create table in procedure or functi...
one thing nikhil handle sqlerrm with variables .that increases perfmance ok.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape