Home » SQL & PL/SQL » SQL & PL/SQL » Use ALter table in PL/SQL block (Oracle 11g)
Use ALter table in PL/SQL block [message #634893] Tue, 17 March 2015 15:30 Go to next message
vmehta
Messages: 1
Registered: March 2015
Junior Member
Hi ,

I have created one simple proc as below ( Oracle PL/SQL )

1 : create or replace procedure alter_table_test
2 : AS
3 : begin
4 : execute immediate 'ALTER TABLE TMP_MAF_2 ADD column col1 varchar(10)';
5 : execute immediate 'ALTER TABLE TMP_MAF_2 ADD column col2 varchar(10)';
6 : end;

The above proc is getting created successfully .. but when I am executing the procedure 'EXEC ALTER_TABLE_TEST' .. it's is showing the error Ora-00904 Invalid identifier at line 4 ... could you please help with this ... i want to alter a table inside a pl/sql proc everytime the procedure is called ... Is there any way to do this task ?
Re: Use ALter table in PL/SQL block [message #634895 is a reply to message #634893] Tue, 17 March 2015 15:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

SQL> create or replace procedure alter_table_test
  2  AS
  3  begin
  4    execute immediate 'ALTER TABLE TMP_MAF_2 ADD column col1 varchar(10)';
  5    execute immediate 'ALTER TABLE TMP_MAF_2 ADD column col2 varchar(10)';
  6  end;
  7  /

Procedure created.

SQL> exec alter_table_test
BEGIN alter_table_test; END;

*
ERROR at line 1:
ORA-00904: : invalid identifier
ORA-06512: at "MICHEL.ALTER_TABLE_TEST", line 4
ORA-06512: at line 1


1/ The table does not exist, so you cannot alter it.

SQL> create table TMP_MAF_2 (id integer);

Table created.

SQL> exec alter_table_test
BEGIN alter_table_test; END;

*
ERROR at line 1:
ORA-00904: : invalid identifier
ORA-06512: at "MICHEL.ALTER_TABLE_TEST", line 4
ORA-06512: at line 1


2/ To debug dynamic query you have to execute it directly:

SQL> ALTER TABLE TMP_MAF_2 ADD column col1 varchar(10);
ALTER TABLE TMP_MAF_2 ADD column col1 varchar(10)
                          *
ERROR at line 1:
ORA-00904: : invalid identifier


3/ The syntax is not correct

SQL> ALTER TABLE TMP_MAF_2 ADD col1 varchar(10);

Table altered.


4/ This is a valid syntax, so fix the procedure code

SQL> create or replace procedure alter_table_test
  2  AS
  3  begin
  4    execute immediate 'ALTER TABLE TMP_MAF_2 ADD col1 varchar(10)';
  5    execute immediate 'ALTER TABLE TMP_MAF_2 ADD col2 varchar(10)';
  6  end;
  7  /

Procedure created.

SQL> drop table TMP_MAF_2;

Table dropped.

SQL> create table TMP_MAF_2 (id integer);

Table created.

SQL> exec alter_table_test

PL/SQL procedure successfully completed.

SQL> desc TMP_MAF_2
 Name                             Null?    Type
 -------------------------------- -------- ----------------------
 ID                                        NUMBER(38)
 COL1                                      VARCHAR2(10 CHAR)
 COL2                                      VARCHAR2(10 CHAR


Re: Use ALter table in PL/SQL block [message #634966 is a reply to message #634895] Wed, 18 March 2015 07:58 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Michel,
Wish I could give you a star. Very nice and through writeup
Previous Topic: CASE in WHERE Clause
Next Topic: Query history and present table
Goto Forum:
  


Current Time: Fri Apr 19 20:47:26 CDT 2024