create or replace trigger insert_test_id
before insert on test
where(test.name='Ash')
begin
insert into test(s_no) values('def');
end
my table is
test id integer name varchar2(200) s_no varchar2(250)
please tell me that what is the error in this trigger. I am not able to find out.
A quick glance at the online documentation would have told you that the conditional syntax is WHEN not WHERE.
You should also reference the column using the NEW keyword rather than the table name. And as Gary rightly points out, we can only apply the conditional clause for ROW LEVEL triggers:
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>
The condition works too...
SQL> insert into t23 values ('Ash')
2 /
1 row created.
SQL> select name from t23
2 /
NAM
---
abc
def
Ash
3 rows selected.
SQL>
It even works for multiple rows....
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>
So what's the problem? This:
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>
Of course I have cheated here, to generate the error. If both the test value and the substituted value are hard-coded the problem can be avoided. But if either is a lookup, then the risk of recursion is there.
If what you actually want to do is replace an input value rather insert an additional row you should use the simple assignment syntax posted by @Lukas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With