触发器中的 :OLD 和 :NEW 变量是什么数据类型?
假设您在 MY_CUSTOMER_TABLE
上有一个触发器,并且它有一个声明为 MY_CUSTOMER_TABLE%ROWTYPE
类型的变量。如何将 OLD
值分配给该变量?
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;
编辑:
澄清一下,这是因为我使用触发器将 MY_CUSTOMER_TABLE
中的行存档到 MY_CUSTOMER_TABLE_HISTORY
中。根据所执行的操作(INSERT
、UPDATE
、DELETE
),我需要来自 OLD
的所有字段或 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;
在这种情况下,PL/SQL 不会让我将 :OLD
或 :NEW
传递给需要 的过程MY_CUSTOMER_TABLE%ROWTYPE
参数。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能。
引用所有列(如 SELECT *)通常是不好的做法,您应该指定所需的列。
You can't.
It is generally bad practice to refer to all columns (like SELECT *) and you should specify the columns you need.
在文档中,您会发现 :old 和 :new 是列值,而不是行类型。因此,您必须手动构建行类型。
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.
据我所知, :NEW 和 :OLD 的定义有点模糊。我看到它被称为对“伪记录”的引用。不过,Oracle 似乎不是在行类型中设置每列都可引用的实际行类型,而是为每个单独的列设置一个引用,然后您可以使用 :NEW 和 :OLD 来引用该列。但是,正如您所发现的, :NEW 和 :OLD 本身似乎不可引用。
例如,此处。 (是的,是的,我知道,这是一个 Java 参考,但请参阅有关 :OLD 本身不是有效参考的评论。
我还发现了此注释
SYS.DBMS_DEBUG
包,这意味着 :NEW/: OLD 也不是有效的绑定。会这个使用
AFTER
触发器的建议对您有用吗?从您的示例来看,似乎没有对值进行任何验证(意识到为了简单起见您可能没有将其放入示例中)。我试图设想一种方法来动态构建一个公共类型(在触发器内),该类型将使用 all_tab_columns 视图匹配您的表行类型,然后将所有值填充到其中,但无法完全理解如果它能起作用的话,它可能会如何发生变化的细节。最终可能会比记录历史记录所需的工作量更多!
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.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!