2

I'm trying to run the following Oracle SQL script which should create a new column, TEMPLATE, and populate it with some base values, but I'm getting an error.

DECLARE  v_cnt INT; 

BEGIN
    select count(*) into v_cnt 
    from user_tab_columns 
    where upper(table_name) = upper('myTable') and upper(column_name) = upper('template');

    IF(v_cnt = 0) THEN
        EXECUTE IMMEDIATE 'ALTER TABLE MYTABLE ADD TEMPLATE NVARCHAR2(255)';

        UPDATE MYTABLE 
        SET TEMPLATE = 'BASE VALUE';
    END IF; 
  END; /

The error that I'm getting is

ORA-06550: line 10, column 37:
PL/SQL: ORA-00904: "TEMPLATE": invalid identifier

When trying to run these statements manually, everything seems to be working fine. I believe the issue is related to the EXECUTE IMMEDIATE portion, with which I'm not familiar. After preliminary Googling I can't determine that this statement is asynchronous or anything like that, so in theory there should be no error thrown there.

2 Answers 2

2

I've found the solution, shortly after posting the question. It is actually a compilation issue. The Oracle compiler reads the entire script before execution and throws an exception because I'm trying to explicitly update a column that, at the time immediately before the script running, does not exist.

The solution was to put the UPDATE statements in EXECUTE IMMEDIATE blocks.

....
EXECUTE IMMEDIATE 'UPDATE EGPL_USER_ROLE SET TEMPLATE = ''BASE VALUE''';
....
Sign up to request clarification or add additional context in comments.

Comments

0

The column name you are attempting to add, TEMPLATE, is a reserved word. See ORA-00904 and see the reserved word list (http://docs.oracle.com/cd/E10530_01/doc/epm.931/html_esb_techref/maxl/ddl/keywords.htm).

Change the name to 'A_TEMPLATE' and it will execute.

3 Comments

I'm not sure if this is the case. I've made the changes as I noted below and everything is working fine.
Yes, I commented out the line, UPDATE MYTABLE SET TEMPLATE = 'BASE VALUE';, when I made my change. I agree with your solution.
That list of keywords is for Hyperion, not the RDBMS. TEMPLATE isn't in the 11gR2 list. (Makes a change from someone linking to MySQL docs though *8-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.