oracle中trigger文档在哪,在Oracle中触发(Trigger in Oracle)

在Oracle中触发(Trigger in Oracle)

我想在oracle中创建一个触发器。 当有人在表上插入时,触发器应该更改insert语句的一个元素。 例:

有人插入:

"User1" "Road2" "Town2" "Age44"

我的触发器应该做:

"User1 "Road2" "Town2 "**Age35**"

但是这不应该是这样的

"after insert on table1 begin update table1 set age='Age35'"

我需要的东西,它立即如此“,而不是插入table1开始插入table1值”用户“”道路“”城市“”Age35“

因此触发器只改变插入的一个元素,其余的相等。 我非常感谢你们可以提供的帮助。 我不知道该怎么做。

I want to create a trigger in oracle. When someone inserts on the table the trigger should change one element of the insert statement. Example:

Someone inserts:

"User1" "Road2" "Town2" "Age44"

my trigger should do:

"User1 "Road2" "Town2 "**Age35**"

But this should not be something like

"after insert on table1 begin update table1 set age='Age35'"

I need something that does it instantly so like "instead of insert on table1 begin insert into table1 values "User" "Road" "Town" "Age35"

so that the trigger only change one element of the insert and the rest is equal. I really appreciate any help you guys can provide. I don't know how to do that.

原文:https://stackoverflow.com/questions/29798080

更新时间:2020-01-27 12:46

最满意答案

您可以在插入之前执行此操作:

create table test (myuser varchar2(100), age number);

create or replace trigger test_trigger

before insert on test

referencing new as new

for each row

begin

if :new.age = 45 then

:new.age := 35;

end if;

end;

/

insert into test values ('user1',45);

insert into test values ('user2',46);

select * from test;

输出:

Table created.

Trigger created.

1 row created.

1 row created.

MYUSER AGE

---------- ----------

user1 35

user2 46

2 rows selected.

编辑:

你也可以使用WHEN 。 但是你不能避免一行一行地去做。 你不会得到整个插入语句,并操纵它。 没有instead of trigger 。

create or replace trigger test_trigger

before insert on test

referencing new as new

for each row

when(new.age = 45)

begin

:new.age := 35;

end;

/

关于性能问题,我认为你应该真的尝试一下。 我不认为这真的会产生负面影响。

You could do it before the insert:

create table test (myuser varchar2(100), age number);

create or replace trigger test_trigger

before insert on test

referencing new as new

for each row

begin

if :new.age = 45 then

:new.age := 35;

end if;

end;

/

insert into test values ('user1',45);

insert into test values ('user2',46);

select * from test;

Output:

Table created.

Trigger created.

1 row created.

1 row created.

MYUSER AGE

---------- ----------

user1 35

user2 46

2 rows selected.

EDIT:

You can also use the WHEN. But you cannot avoid doing it row by row. You do not get the whole insert statement somehow and manipulate on it. There is no instead of trigger for tables.

create or replace trigger test_trigger

before insert on test

referencing new as new

for each row

when(new.age = 45)

begin

:new.age := 35;

end;

/

Regarding the performance concerns I think you should really try it out. I do not think it will be so a negative impact really.

2015-04-22

相关问答

你是否在同一时间获得所有这些错误,或者在尝试不同的事情时遇到不同的错误? 如果您在NEW.product_id之前省略: ORA-00904(可能与之关联的ORA-00933),PLS-00801可能来自两者之间的空间 (即: NEW.product_id 。不知道如何在同一时间获得两者。 因为它现在发布它看起来很好 - 你仍然得到消息Errors: check compiler log TRIGGER T1 compiled后Errors: check compiler log - 或者你在SQ

...

插入记录时, OLD每个字段都将为NULL ,包括表定义中标记为NOT NULL的字段。 例如,假设您的表具有名为id的非可空列: CREATE TABLE some_table (

id NUMBER NOT NULL,

foo VARCHAR2(100)

)

将记录插入此表时, OLD.id将为NULL 。 但是,在此表中更新记录时, OLD.id将不为NULL 。 因为您只想更改:NEW.foo如果正在更新记录,您只需检查OLD.id是否具有非空值。 CREATE OR R

...

触发器类似于包或程序,因此您可以简单地使用 create or replace trigger triggerName

...

declare

...

begin

...

end;

A trigger is similar to a package or a procedure, so you can simply use create or replace trigger triggerName

...

declare

...

begin

...

end;

您可以在插入之前执行此操作: create table test (myuser varchar2(100), age number);

create or replace trigger test_trigger

before insert on test

referencing new as new

for each row

begin

if :new.age = 45 then

:new.age := 35;

end if;

end;

/

insert into te

...

我现在无法测试,但我会尝试类似的东西 create or replace trigger secondary_tasks_bi

before insert or update on secondary_tasks for each row

declare

v_dummy varchar2(1);

begin

select null

into v_dummy

from tasks

where code = :new.code_primary

and

...

变异表错误是代码气味。 它几乎总是指向一个糟糕的数据模型,通常标准化不足。 当然,这里有一个糟糕的数据模型。 表上有一列有两个设置。 没关系。 现在,您要将同一列添加到第二个表,并使两者保持同步。 这个新专栏完全没有意义。 新列中没有可用的信息,而不是从查询第一个表中获得的信息。 这就是ORA-04091告诉你的。 你可以花费大量的时间来构建一个变通方法,但这都会浪费精力。 The mutating table error is a code smell. It almost always poi

...

根据你的描述: 尝试重新编译触发器,看看会发生什么...... 如果基础对象(ex..table)变为无效或已更改且触发器引用受影响的表,则触发器将变为无效。 From what you described: Try to recompile the trigger and see what happens... A trigger becomes invalid if the base object (ex..table) becomes invalid or altered and the t

...

如果您无法触摸应用程序代码并且应用程序本身未将此信息传递给数据库,那么您将陷入僵局。 将该信息提供给后端代码的唯一方法是让中间层传递它。 Oracle为应用程序提供了许多方法,可以将信息从中间层传递到后端,但必须构建应用程序才能利用它们。 例如, DBMS_APPLICATION_INFO包具有set_client_info过程,该过程允许中间层传递您的后端触发器可以查询的中间层用户的名称。 如果需要更通用的机制,也可以使用Oracle上下文 。 但是,这些方法中的任何一种实际上都要求编写Java

...

我很确定,对于以前的ORACLE版本,这个szenario已经有效了。 它没有。 我在11gR2中运行了你的代码并获得了相同的结果: set serveroutput on

update TRIGGER_TEST set COL1 = 'now we will see';

here we are in TR_TRIGGER_TEST_1

1 row updated.

select * from TRIGGER_TEST;

COL1 C

...

是否可以在特定程序开始执行之前触发触发器? 不,您不能在没有任何DML操作的情况下手动触发触发器。 触发器旨在隐式执行任何DML操作。 触发器是动作的( 侧面 )效果,而不是动作本身。 我认为你不需要触发你的要求。 您可以在执行AB 程序之前调用过程。 相应地设置您的业务逻辑。 PL / SQL是一种过程语言。 所以,如果你把另一个程序说成AB之前的程序C ,那么AC将在AB之前执行。 is it possible to fire a trigger before a specific proce

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值