Solved

using ALTER TABLE in ORACLE 9i to RENAME NOT NULL CONSTRAINTS IN A LOOP

Posted on 2004-08-12
2
7,324 Views
Last Modified: 2012-06-21
I am getting an error message in the following SQL at the two lines where I invoke ALTER.  What I need to do is rename all NOT NULL constraints in my database to a more meaningful name.  Can someone help me with this?  I need to get this done quick!

DROP TABLE TEMP_CONSTRAINT_RENAMER;
CREATE TABLE TEMP_CONSTRAINT_RENAMER (
aTABLE_NAME VARCHAR2(32),
aCOLUMN_NAME VARCHAR2(32),
aOLD_CONSTRAINT_NAME VARCHAR2(32),
aNEW_CONSTRAINT_NAME VARCHAR2(32)
);

INSERT INTO TEMP_CONSTRAINT_RENAMER (aTABLE_NAME,aCOLUMN_NAME,aOLD_CONSTRAINT_NAME,aNEW_CONSTRAINT_NAME)
SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, 'NN_' || substr(TABLE_NAME,6,12) || '_' || substr(COLUMN_NAME,1,12) || '_' ||  ROWNUM AS NEW_CONSTRAINT_NAME
FROM USER_CONS_COLUMNS
WHERE CONSTRAINT_NAME LIKE 'SYS%';

CREATE OR REPLACE PROCEDURE TEMP_MAKE_NN_NAMES IS

CURSOR C1 IS SELECT ATABLE_NAME,ACOLUMN_NAME,AOLD_CONSTRAINT_NAME,ANEW_CONSTRAINT_NAME FROM TEMP_CONSTRAINT_RENAMER;

myTABLE_NAME VARCHAR(32);
myCOLUMN_NAME VARCHAR(32);
myOLD_CONSTRAINT_NAME VARCHAR(32);
myNEW_CONSTRAINT_NAME VARCHAR(32);

BEGIN
  FETCH C1 INTO myTABLE_NAME, myCOLUMN_NAME, myOLD_CONSTRAINT_NAME, myNEW_CONSTRAINT_NAME;
  WHILE C1%FOUND LOOP
    /* THE FOLLOWING TWO LINES OF CODE WILL NOT COMPILE!  HELP!
     ALTER TABLE myTABLE_NAME DROP CONSTRAINT myOLD_CONSTRAINT_NAME;
     ALTER TABLE myTABLE_NAME MODIFY myCOLUMN_NAME CONSTRAINT myNEW_CONSTRAINT_NAME NOT NULL;
     FETCH C1 INTO myTABLE_NAME, myCOLUMN_NAME, myOLD_CONSTRAINT_NAME, myNEW_CONSTRAINT_NAME;
  END LOOP;
  CLOSE C1;
END TEMP_MAKE_NN_NAMES;

/* EXEC TEMP_MAKE_NN_NAMES;
0
Comment
Question by:rdytmire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
mudumbe earned 500 total points
ID: 11785442
There are 2 problems:

1.  You need OPEN c1 stmt before the first fetch.
2.  In order to your ALTER TABLE, the easiest way is to use EXECUTE IMMEDIATE.  PL/SQL does not support the way you have constructed ALTER TABLE

     execute immediate 'ALTER TABLE ' || myTABLE_NAME || ' DROP CONSTRAINT ' ||
     myOLD_CONSTRAINT_NAME;
     execute immediate 'ALTER TABLE '  ||myTABLE_NAME || ' MODIFY ' ||
     myCOLUMN_NAME || '  CONSTRAINT ' || myNEW_CONSTRAINT_NAME || ' NOT NULL';

NOTE the extra spaces around the quoted string (keywords) to avoid runtime sql syntax errors.
0
 
LVL 1

Author Comment

by:rdytmire
ID: 11785610
Thanks.. this solved the problem.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question