Solved

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

Posted on 2004-08-12
2
7,282 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

822 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