using Exicute Immediate and :NEW :OLD in database trigger
KMcSmartMar 20 2008 — edited Mar 28 2008Hello,
I think I am having trouble using :NEW in an EXECUTE IMMEDIATE call in a database INSTEAD OF trigger when the string ":NEW" is being passed as a bind variable.
I am trying to do the following to create a Forms "NAME_IN" type of functionality in a database trigger:
IF INSERTING THEN
FOR x IN(SELECT column_name new_rating
FROM user_tab_columns
WHERE table_name = 'V_RATING_PRC_RECOM'
AND column_name LIKE 'RATING%'
AND INSTR(column_name,'_') = 0
ORDER BY 1)
LOOP
v_rating_column_name := ':NEW.'||x.new_rating;
v_stmt_str := 'SELECT :rating_column_name INTO v_rating FROM DUAL';
EXECUTE IMMEDIATE v_stmt_str USING v_rating_column_name;
END LOOP;
END;
I get the error message ORA-00905: missing keyword. I think :NEW is being seen as a bind variable, not as a :NEW database value in the EXECUTE IMMEDIATE call.
My SELECT from the user_tab_columns returns the following:
RATING1
RATING2
RATING3
RATING4... etc
I then want to pass them as the following bind variables to my EXECUTE IMMEDIATE call
:NEW.RATING1
:NEW.RATING2
:NEW.RATING3
:NEW.RATING4...etc
So that I can retrieve the values in each of the rating columns and store them in v_rating to be used later in the loop. ie
v_rating = 3.4 for :NEW.RATING1
v_rating = 3.6 for :NEW.RATING2
v_rating = 3.7 for :NEW.RATING3
v_rating = 3.1 for :NEW.RATING4....etc
BTW, once I have the v_rating values then I insert them each as rows in a table. The INSTEAD OF database trigger that I am unpivoting a table (converting columns to rows) and I need to loop through the column names (rating1, rating2, rating3 etc) to retrieve the values in each of the columns. I know I can just create many, many if statements but I would rather create code that will work for any number of rating1, rating2 etc columns in the table.
Of course the following works just fine
SELECT :NEW.rating1 INTO v_rating FROM DUAL;
I just can't seem to generate that statement as dynamic SQL with :NEW.rating1 changing for each execution of the loop.
My real question is how can I pass ":NEW" as a bind variable to an execute immediate command in a database trigger.
Thanks
Karen