开发者

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

开发者 https://www.devze.com 2023-03-04 07:02 出处:网络
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?

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( sou开发者_JS百科rce_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.


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


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;


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_DEBUG package which implies that :NEW/:OLD is not a valid bind either.

-- get_value and set_value now support bind names. Bind names must be -- put in quotes and capitalized. Note that trigger binds have -- qualified names, i.e. ":NEW" is not a valid bind, while ":NEW.CLMN" -- is valid.

Would this suggestion of using an AFTER trigger 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消