I am trying to do something apparently simple, but am struggling.
I need to renumber every primary and foreign key in a database. I am using surrogate keys, all integers generated by a trigger which gets the NEXTVAL from a sequence. There is one trigger and matching sequence for every table in the database. All key fields end in '_ID', and can therefore be identified from USER_TAB_COLUMNS.
I am attempting this via sql plus, as I am required to deliver a scripted solution, which requires that the user need have no knowledge of Oracle. The technicalities of doing the renumbering are easy, what I can't do is successfully get the user to enter a number into sql plus, and then use that number in an anonymous pl/sql block.
Here is the relevant part of my script
I want to use the number entered by the user as the variable called start_offset. The above does not work, and I neglected to bring the errors home with me from work - my apologies. Nonetheless, I still post the question.
I suspect that using a bind variable would be more efficient, so I would also welcome advice regarding this.
Note that I have hard-coded start_offset as 5, in order to attempt to solve the problem. It should be whatever value the user has entered.
A debug in progress...
Regards
Tharg
Grinding away at things Oracular
I need to renumber every primary and foreign key in a database. I am using surrogate keys, all integers generated by a trigger which gets the NEXTVAL from a sequence. There is one trigger and matching sequence for every table in the database. All key fields end in '_ID', and can therefore be identified from USER_TAB_COLUMNS.
I am attempting this via sql plus, as I am required to deliver a scripted solution, which requires that the user need have no knowledge of Oracle. The technicalities of doing the renumbering are easy, what I can't do is successfully get the user to enter a number into sql plus, and then use that number in an anonymous pl/sql block.
Here is the relevant part of my script
Code:
--Show available departments to user
SELECT DEPARTMENT_ID, CODE, DESCRIPTION
FROM DEPARTMENT
ORDER BY DEPARTMENT_ID;
PROMPT 'Enter the department number you wish to use:-'
ACCEPT department_number
VAR start_offset NUMBER(1)
BEGIN :start_offset := 5; END;
BEGIN
FOR X IN
(SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLUMNS
WHERE SUBSTR(COLUMN_NAME,-3,3) = '_ID'
AND SUBSTR(TABLE_NAME,1,2) != 'V_'
ORDER BY TABLE_NAME, COLUMN_NAME)
LOOP
EXECUTE IMMEDIATE ( 'UPDATE '||X.TABLE_NAME||'SET '||X.COLUMN_NAME
||'= ('||X.COLUMN_NAME||'* 100)+'||:start_offset);
END LOOP;
END;
/
COMMIT;
I want to use the number entered by the user as the variable called start_offset. The above does not work, and I neglected to bring the errors home with me from work - my apologies. Nonetheless, I still post the question.
I suspect that using a bind variable would be more efficient, so I would also welcome advice regarding this.
Note that I have hard-coded start_offset as 5, in order to attempt to solve the problem. It should be whatever value the user has entered.
A debug in progress...
Regards
Tharg
Grinding away at things Oracular