0

The question is as follows: create a stored procedure (NEW_TABLE) with two strings as input parameters:

  • String 1: Name of the table to be generated
  • String 2: Name of columns and their datatypes.

For example: 'ID Number, ProductName varchar2(50), Quantity'

I implemented the following code:

CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
    (TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
is
begin
    EXECUTE IMMEDIATE
    'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
end;

I called it as follows:

EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY')

The procedure was created without compilation error, but when I executed this procedure there was error

missing right parenthesis

Kindly share how can I resolve this. I'm only required to take two strings as input parameters for procedure.

6
  • We will not answer this until you show what is actual parameter values are of the failing call Commented Aug 14, 2021 at 4:21
  • 1
    your column QUANTITY does not have data type Commented Aug 14, 2021 at 4:40
  • I added the datatype still same error. Commented Aug 14, 2021 at 4:42
  • 2
    your VARCAR2 is not declared properly. I can fiddle it just fine Commented Aug 14, 2021 at 4:48
  • 1
    While this obvious homework assignment may be able to teach several minor bits, I do hope that the lessons is NOT that this is a good way to create tables. As a business requirement, it is atrocious. It is very very VERY rare to have a justification for using dynamic SQL to create objects in the database. Commented Aug 14, 2021 at 11:45

1 Answer 1

2

When working with dynamic SQL, always display what you're going to execute (using dbms_output.put_line). Only if it is OK, then execute (immediate) it.

SQL> CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
  2      (TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
  3  is
  4    l_str varchar2(200);
  5  begin
  6    l_str :=
  7      'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
  8    dbms_output.put_line(l_str);
  9    --execute immediate l_str;     --> don't run it until you make sure L_STR is correct
 10  end;
 11  /

Procedure created.

Testing:

SQL> set serveroutput on
SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY)    --> this will be executed

PL/SQL procedure successfully completed.

SQL>

Statement you're about to execute is

CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY)
                                              -------              ---
                                              typo             missing datatype

Does it look OK to you? Doesn't to me (and other as well).

When fixed:

SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER)

PL/SQL procedure successfully completed.

SQL>

Does CREATE TABLE look OK now? Yes, it does. So uncomment EXECUTE IMMEDIATE from the procedure and repeat everything:

SQL> CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
  2      (TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
  3  is
  4    l_str varchar2(200);
  5  begin
  6    l_str :=
  7      'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
  8    dbms_output.put_line(l_str);
  9    execute immediate l_str;
 10  end;
 11  /

Procedure created.

SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER)

PL/SQL procedure successfully completed.

SQL> DESC PRODUCTS
 Name                                                  Null?    Type
 ----------------------------------------------------- -------- ----------------------------------
 ID                                                             NUMBER
 PRODUCT_NAME                                                   VARCHAR2(50)
 QUANTITY                                                       NUMBER

SQL>

The table is now created.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.