Search code examples
oraclestored-proceduresplsql

This is regarding maintain details using a procedure. Can someone help me to do this question by using stored procedures. I'm a beginner to plsql


Its asked to create a table to maintain details by using a stored procedure. Do I need to use a procedure to create the table or do I need to use a procedure to insert values?

  1. Create a table to maintain company details by using a stored procedure by passing company id and name as input parameters.

    Company ID Name
    COMP_A Company A
    COMP_B Company B

Solution

  • Creating a table from PL/SQL is possible - you'd need dynamic SQL for that. But, you should avoid it because that's simply bad practice: create table at SQL level, then write a procedure which will manipulate data stored within:

    create table some_name (id number, name varchar2(20));
    
    create or replace procedure p_some_proc (par_id in number, par_name in varchar2)
    is
    begin
      insert into some_name (id, name) values (par_id, par_name);
    end;
    /
    
    begin
      p_some_proc(par_id => 1, par_name => 'Littlefoot');
    end;
    /
    

    If you'd want to do create table in a procedure, then you'd

    create or replace procedure p_some_proc as
    begin
      execute immediate 'create table some_name (id number, name varchar2(20))';
    end;
    /
    
    begin
      p_some_proc;
    end;
    /
    

    Note that such a procedure would fail every time you call it if table already exists so you'd have to handle the exception. Not that it is impossible, but - that's just not the way to do it.