Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Of what data type are the :OLD and :NEW variables in a trigger?

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.

like image 585
Adam Paynter Avatar asked May 09 '11 23:05

Adam Paynter


People also ask

In which trigger both new and old data can be accessed?

Additionally, for row trigger, the statements in a trigger action have access to column values (new and old) of the current row being processed by the trigger.

Can we use old and new values in statement trigger?

A trigger fired by an INSERT statement has meaningful access to new column values only. Because the row is being created by the INSERT , the old values are null. A trigger fired by an UPDATE statement has access to both old and new column values for both BEFORE and AFTER row triggers.

Which of the following trigger uses the old and new qualifiers?

The OLD and NEW qualifiers are used to reference the values of a column before and after the data change, respectively. The OLD and NEW qualifiers can be used only with row triggers.


2 Answers

You can't. It is generally bad practice to refer to all columns (like SELECT *) and you should specify the columns you need.

like image 60
Gary Myers Avatar answered Sep 30 '22 21:09

Gary Myers


In the docs you will find that :old and :new are column values, not row types. So you will have to construct your rowtype manually.

trigger ....
  l_row  mytable%rowtype;
begin
  l_row.column1 := :old.column1;
  l_row.column2 := :old.column2;
  ...

  archive_function(l_row);
end;
like image 22
Rene Avatar answered Sep 30 '22 21:09

Rene