Suppose you have a trigger on MY_CUSTOMER_TABLE and that it has a variable declared of type MY_CUSTOMER_TABLE%ROWTYPE. How can I assign the OLD value into that variable?
CREATE TRIGGER CUSTOMER_BEFORE
BEFORE UPDATE ON MY_CUSTOMER_TABLE
FOR EACH ROW
DECLARE
old_version MY_CUSTOMER_TABLE%ROWTYPE;
BEGIN
old_version := :OLD; /* Causes a PLS-00049 bad bind variable 'OLD' */
old_version := OLD; /* Causes a PLS-00201 identifier 'OLD' must be declared */
END;
Edit:
To clarify, this came about because I am using triggers to archive rows from MY_CUSTOMER_TABLE into MY_CUSTOMER_TABLE_HISTORY. Depending upon the action being performed (INSERT, UPDATE, DELETE), I need all the fields from either OLD or NEW:
CREATE TRIGGER CUSTOMER_BEFORE
BEFORE UPDATE ON MY_CUSTOMER_TABLE
FOR EACH ROW
DECLARE
historical_record MY_CUSTOMER_TABLE_HISTORY%ROWTYPE;
PROCEDURE
copy
(
source_record MY_CUSTOMER_TABLE%ROWTYPE,
destination_record IN OUT MY_CUSTOMER_TABLE_HISTORY%ROWTYPE
)
BEGIN
destination_record.customer_id := source_record.customer_id;
destination_record.first_name := source_record.first_name;
destination_record.last_name := source_record.last_name;
destination_record.date_of_birth := source_record.date_of_birth;
END;
BEGIN
/* I didn't want to replicate the same assignment statements for
each of the two cases: */
CASE
WHEN INSERT OR UPDATING THEN
copy( source_record => :NEW, destination_record => historical_record );
WHEN DELETING THEN
copy( source_record => :OLD, destination_record => historical_record );
END CASE;
/* Some other assignments to historical_record fields... */
INSERT INTO MY_CUSTOMER_TABLE_HISTORY VALUES historical_record;
END;
In this scenario, PL/SQL will not let me pass :OLD or :NEW to a procedure that expects a MY_CUSTOMER_TABLE%ROWTYPE argument.
From what I can determine, the definition of what :NEW and :OLD really are IS a little vague. I have seen it referred to as a reference to a “pseudo-record”. It would seem though, that instead of an actual rowtype with each column being reference-able within the rowtype, Oracle sets up a reference to each individual column, which you then reference by using :NEW and :OLD. But, as you are finding out, :NEW and :OLD don’t seem to be referenceable by themselves.
For example, here. (yes yes, I know, it’s a Java reference, but see the comment about :OLD not being a valid reference by itself.
I also found this note
SYS.DBMS_DEBUGpackage which implies that :NEW/:OLD is not a valid bind either.Would this suggestion of using an
AFTERtrigger work for you? From your example, it doesn’t seem that there is any validation happening on the values (realizing you may not have put that into your example for simplicity).I was trying to envision a way to construct a public type on the fly (within your trigger) that would match your table rowtype using the all_tab_columns view, and then stuff all the values into it, but can’t quite wrap my head around the details of how that might shake out…if it would even work. And it likely ends up being more work than required to log a historical record!