I'm writing a script that will add an ID column to a table, including all the sequences and triggers to keep it automatically up to date. It will perform the following:
- Add a column to a table
- Create a sequence
- Create a trigger
- Update existing rows to use sequence values
- Set the field to not nullable
The problem I have is with the line in bold - it would be an UPDATE
statement in a DDL script. The error I get is:
PLS-00103: Encountered the symbol "UPDATE"
I've tried wrapping the UPDATE
in a BEGIN
and END
block, with no success.
Is it possible to include an UPDATE
statement in a DDL script?
Here's what I have so far:
ALTER TABLE RETAILER ADD (RETAILER_ID NUMBER );
CREATE SEQUENCE RETAILER_ID_SEQ;
CREATE OR REPLACE TRIGGER RETAILER_ADD_TRG
BEFORE INSERT ON RETAILER
FOR EACH ROW
BEGIN
SELECT RETAILER_ID_SEQ.NEXTVAL INTO :new.RETAILER_ID FROM dual;
END;
-- Doesn't like this part...
UPDATE RETAILER SET RETAILER_ID = RETAILER_ID_SEQ.NEXTVAL;
COMMIT;
ALTER TABLE RETAILER MODIFY (RETAILER_ID NOT NULL);