Home » SQL & PL/SQL » SQL & PL/SQL » ALTER TABLE in PL/SQL procedure
ALTER TABLE in PL/SQL procedure [message #115774] Thu, 14 April 2005 06:41 Go to next message
shooter
Messages: 44
Registered: February 2005
Member
I am working on Oracle 9i.

I wrote and compiled the following function:

create or replace procedure altermytable is 
begin
  
  alter table mytable disable constraint mytable_fk;

end;


Compilation returned the error PLS-00103 "Encountered the symbol ALTER when expecting one of the following..."

What is the problem?

How can I use ALTER TABLE statements in my PL/SQL procedures?

Re: ALTER TABLE in PL/SQL procedure [message #115788 is a reply to message #115774] Thu, 14 April 2005 08:25 Go to previous messageGo to next message
saadatahmad
Messages: 452
Registered: March 2005
Location: Germany/Paderborn
Senior Member

You cannot perform Alter command this way in PL/SQL.
You need to use here Dynamic SQl.
The Procedure is as follows.


SQL> CREATE OR REPLACE PROCEDURE proc_modify
  2     IS
  3     cur_hdl         INTEGER;
  4     stmt_str        VARCHAR2(200);
  5     rows_processed  BINARY_INTEGER;
  6  BEGIN
  7     stmt_str := 'ALTER TABLE testing MODIFY a NUMBER';
  8     -- open cursor
  9     cur_hdl := dbms_sql.open_cursor;
 10     -- parse cursor
 11     dbms_sql.parse(cur_hdl, stmt_str,
 12        dbms_sql.native);
 13      -- execute cursor
 14      rows_processed :=
 15      dbms_sql.execute(cur_hdl);
 16      -- close cursor
 17      dbms_sql.close_cursor(cur_hdl);
 18  END;
 19  /

Procedure created.

SQL> desc testing
 Name                            Null?    Type
 ------------------------------- -------- ----
 A                                        VARCHAR2(32)

SQL> EXEC proc_modify

PL/SQL procedure successfully completed.

SQL> desc testing
 Name                            Null?    Type
 ------------------------------- -------- ----
 A                                        NUMBER



Well, you can modify the Alter command here as per your requirement.

Regards!
Re: ALTER TABLE in PL/SQL procedure [message #115790 is a reply to message #115788] Thu, 14 April 2005 08:30 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
A little bit shorter way is to use execute immediate statement:

create or replace procedure altermytable is
begin

execute immediate 'alter table mytable disable constraint mytable_fk';

end;

Rgds.

[Updated on: Thu, 14 April 2005 08:31]

Report message to a moderator

Re: ALTER TABLE in PL/SQL procedure [message #115791 is a reply to message #115790] Thu, 14 April 2005 08:36 Go to previous messageGo to next message
saadatahmad
Messages: 452
Registered: March 2005
Location: Germany/Paderborn
Senior Member

I think this will work only in 9i and above.
However, EXECUTE IMMEDIATE is the simplest one. Smile
Re: ALTER TABLE in PL/SQL procedure [message #115793 is a reply to message #115791] Thu, 14 April 2005 08:48 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
SQL> create table z_tmp (id number(10));

Table created.

SQL> edit
Wrote file afiedt.buf

  1  begin
  2   execute immediate 'alter table z_tmp modify (id number(11))';
  3* end;
SQL> /

PL/SQL procedure successfully completed.

SQL> desc z_tmp;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER(11)

SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production
PL/SQL Release 8.1.7.0.0 - Production
CORE    8.1.7.0.0       Production
TNS for Linux: Version 8.1.7.0.0 - Development
NLSRTL Version 3.4.1.0.0 - Production


As you can easily see, it works in 8i.

Rgds.
Re: ALTER TABLE in PL/SQL procedure [message #115794 is a reply to message #115793] Thu, 14 April 2005 08:51 Go to previous message
saadatahmad
Messages: 452
Registered: March 2005
Location: Germany/Paderborn
Senior Member

Thanks dmitry. I was not aware of this. I'm using Oracle 9i.

Thanks again.
Previous Topic: Getting values from user during the execution of a package
Next Topic: Joining 10 remote tables - Performance
Goto Forum:
  


Current Time: Sun Aug 31 01:01:03 CDT 2025