Nicky Nick
Nicky Nick

Reputation: 135

Invalid identifier error while running plsql block

Since my school does not allow me to post the code, hence i had to come back home and put up an example to show the issue i am facing. My school asked me to do a homework on dynamic sql to create a table and later insert one dummy record to it. But while doing it I am facing the below issue. Please find the code below for your reference. Thank you.

Procedure:

create or replace procedure table_creation(table_name in varchar2,col1  varchar2,col2 varchar2)
is
l_stat varchar2(3000);
v_stat varchar2(1000);
a varchar2(10):='1';
b varchar2(10):='Dummy';
begin
l_stat:='create table '||table_name||' ("'||col1||'"   varchar2(10),"'||col2||'" varchar2(10))';
execute immediate l_stat;
execute immediate 'insert into '||table_name||'('||col1||','||col2||')  values    (:x,:y)' using a,b;
end;
/

Plsql Block to call the procedure:

set serveroutput on;
declare
a varchar2(10);
b varchar2(10);
c varchar2(10);
begin
a:='Example';
b:='id';
c:='nm';
table_creation(a,b,c);
dbms_output.put_line('Yes');
end;
/

The procedure is getting created perfectly and while running the above block i am getting the below error .

declare
*
ERROR at line 1:
ORA-00904: "NM": invalid identifier
ORA-06512: at "SYS.TABLE_CREATION", line 9
ORA-06512: at line 9

But when I checked the created table. The table exists with 0 records. The structure of the table is as follows.

 Name                                      Null?    Type
 ----------------------------------------- -------- ---------------
 ID                                                 VARCHAR2(10)
 NM                                                 VARCHAR2(10)

Any help from your end is much appreciated.

Upvotes: 4

Views: 766

Answers (1)

Vimal Bhaskar
Vimal Bhaskar

Reputation: 778

Nick,

Please note that the above procedure will create the column with a double qoutes ("ID","NM") hence the error occurred. Please find the updated code and check if the issue has resolved.

According to oracle=>

Oracle is case sensitive in column and table names. It just converts everything to upper case by default. But if you use names in double quotes, you tell Oracle to create the column in the exact spelling you provided (lower case in the CREATE statement).

Code:

create or replace procedure table_creation(table_name in varchar2,col1 varchar2,col2 varchar2)
is
l_stat varchar2(3000);
v_stat varchar2(1000);
a varchar2(10):='1';
b varchar2(10):='Dummy';
begin
l_stat:='create table '||table_name||' ('||col1||' varchar2(10),'||col2||' varchar2(10))';
execute immediate l_stat;
execute immediate 'insert into '||table_name||'('||col1||','||col2||') values   (:x,:y)' using a,b;
end;
/

Note: I have not touched any other logic of the code. Please try and let us know the result.

Only change is

From :

 l_stat:='create table '||table_name||' ("'||col1||'" varchar2(10),"'||col2||'" varchar2(10))';

to :

 l_stat:='create table '||table_name||' ('||col1||' varchar2(10),'||col2||' varchar2(10))';

Upvotes: 4

Related Questions