gpt4 book ai didi

oracle - 条件触发

转载 作者:行者123 更新时间:2023-12-01 09:06:56 24 4
gpt4 key购买 nike

create or replace trigger insert_test_id
before insert on test
where(test.name='Ash')
begin
insert into test(s_no) values('def');
end

我的 table 是

测试id 整数名称 varchar2(200)s_no varchar2(250)

请告诉我这个触发器的错误是什么。我查不出来。

最佳答案

快速浏览online documentation会告诉你条件语法是 WHEN 而不是 WHERE。

您还应该使用 NEW 关键字而不是表名来引用列。正如 Gary 正确指出的那样,我们只能将条件子句应用于 ROW LEVEL 触发器:

SQL> create or replace trigger insert_test_id
2 before insert on t23
3 for each row
4 when (new.name='Ash')
5 begin
6 insert into t23(name) values('def');
7 end;
8 /

Trigger created.

SQL> insert into t23 values ('abc')
2 /

1 row created.

SQL> select name from t23
2 /

NAM
---
abc

1 rows selected.

SQL>

条件也有效...

SQL> insert into t23 values ('Ash')
2 /

1 row created.

SQL> select name from t23
2 /

NAM
---
abc
def
Ash

3 rows selected.

SQL>

它甚至适用于多行......

SQL> insert into t23
2 select txt from t42
3 /

4 rows created.

SQL> select name from t23
2 /

NAM
---
abc
def
Ash
XXX
ZZZ
ABC
DEF

7 rows selected.

SQL>

那么问题是什么?这个:

SQL> create or replace trigger insert_test_id
2 before insert on t23
3 for each row
4 when (new.name='def')
5 begin
6 insert into t23(name) values('def');
7 end;
8 /

Trigger created.

SQL> insert into t23 values ('def')
2 /
insert into t23 values ('def')
*
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger 'APC.INSERT_TEST_ID'
ORA-06512: at "APC.INSERT_TEST_ID", line 2
ORA-04088: error during execution of trigger


SQL>

我当然在这里作弊,产生错误。如果测试值和替换值都被硬编码,则可以避免该问题。但是,如果其中任何一个是查找,则存在递归风险。


如果您真正想做的是替换输入值而不是插入额外的行,您应该使用 simple assignment syntax posted by @Lukas .

关于oracle - 条件触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6173556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com