Database Trigger Error [message #411559] |
Sun, 05 July 2009 06:51  |
mamalik
Messages: 270 Registered: November 2008 Location: Pakistan
|
Senior Member |

|
|
Dear All
I have following trigger on table but it is creating error during insertion in table.
CREATE TABLE A
(
IDE NUMBER
);
Insert Into A Values(1);
Commit;
CREATE TABLE ORA_FAQ_ONE
(
IDE NUMBER,
NAME VARCHAR2(250 BYTE)
);
ALTER TABLE ORA_FAQ_ONE ADD (
CONSTRAINT ORA_FAQ_ONE_PK PRIMARY KEY (IDE)
I have following trigger on ORA_FAQ_ONE Table
CREATE OR REPLACE TRIGGER TRG_ORA_FAQ_ONE BEFORE INSERT ON ORA_FAQ_ONE
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
DECLARE
v_id NUMBER;
BEGIN
SELECT NVL(MAX(v_id),0)+1 INTO v_id FROM a;
:NEW.Ide:=v_id;
UPDATE a
SET ide=v_id;
END;
After this I am running following query to insert data
INSERT INTO ora_faq_one(name)
SELECT 'abc' FROM dual
UNION
SELECT 'def' FROM dual
After this following error is occuring
Ora-00001. Unique Contraint Voilated.
I am unable to understand why this is occuring while "Before insert for each row" trigger fire for each row before inserting and i am assinging new value to "Id" for each row.
Thanks In Advance
Regards
Asif.
|
|
|
Re: Database Trigger Error [message #411560 is a reply to message #411559] |
Sun, 05 July 2009 07:03   |
 |
vamsi kasina
Messages: 2112 Registered: October 2003 Location: Cincinnati, OH
|
Senior Member |
|
|
SELECT NVL(MAX(v_id),0)+1 INTO v_id FROM a; Is your intention to writeSELECT NVL(MAX(IDE),0)+1 INTO v_id FROM a; Anyway why can't you use sequence to fill the ORA_FAQ_ONE,IDE?
EDIT:
SELECT IDE + 1 INTO v_id FROM a; I think this is also sufficient.
By
Vamsi
[Updated on: Sun, 05 July 2009 07:17] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Database Trigger Error [message #411610 is a reply to message #411605] |
Mon, 06 July 2009 00:54   |
 |
Michel Cadot
Messages: 68737 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
SQL> create table t (id number, nam varchar2(50), ts timestamp default systimestamp);
Table created.
SQL> create or replace procedure p (id in number, nam in varchar2)
2 is
3 pragma autonomous_transaction;
4 begin
5 insert into t (id, nam) values (id, nam);
6 commit;
7 end;
8 /
Procedure created.
SQL> CREATE OR REPLACE TRIGGER trg_ora_faq_one BEFORE INSERT
2 ON ora_faq_one
3 REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
4 DECLARE
5 v_id NUMBER;
6 BEGIN
7 SELECT NVL (v_id, 0) + 1
8 INTO v_id
9 FROM a;
10
11 p(v_id, :new.name);
12 :NEW.ide := v_id;
13
14 UPDATE a
15 SET ide = v_id;
16 END;
17 /
Trigger created.
SQL> Insert Into Ora_Faq_One(Name)
2 Select 'Abc' From Dual
3 Union
4 Select 'Def' From Dual;
Insert Into Ora_Faq_One(Name)
*
ERROR at line 1:
ORA-00001: unique constraint (MICHEL.ORA_FAQ_ONE_PK) violated
SQL> select * from t;
ID NAM TS
---------- ---------- ------------------------------
1 Abc 06-JUL-09 07.52.05.090000 AM
1 Def 06-JUL-09 07.52.05.090000 AM
Regards
Michel
|
|
|
|
|
|
|
|
|
|
Re: Database Trigger Error [message #411705 is a reply to message #411700] |
Mon, 06 July 2009 06:46   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Or, to paraphrase Michel, an INSERT ... SELECT will insert a set of rows at once, in a single transaction (ie the rows will all succeed or fail together).
A row level trigger will fire for each individual row in this set.
|
|
|
Re: Database Trigger Error [message #411707 is a reply to message #411705] |
Mon, 06 July 2009 06:49   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
I'd have to suggest that if you don't even know things like this, then you're probably not ready to be doing tasks like Creating Triggers.
|
|
|
Re: Database Trigger Error [message #413839 is a reply to message #411559] |
Fri, 17 July 2009 15:32  |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
Your whole approach is wrong. If two people are inserting at the same time, they will get the same key generated. This is what sequences are for. Start the sequence at 1 and let it increment forever. Have an indexed time stamp column. The time stamp column will show you every row that is inserted during the year. Putting intelligence into the sequence is wasteful, unusable and silly. I have been programming for 30 years and can say that your design is bad. if you want to know all the rows and the order that they were entered for January of 2009 simply
select col
from my_table
where entry_date BETWEEN TO_DATE('01-JAN-2009','DD-MON-YYYY') AND TO_DATE('31-JAN-2009 24:59.59','DD-MON-YYYY HH24:MI.SS')
ORDER BY MY_SEQUENCE;
[Updated on: Fri, 17 July 2009 15:34] Report message to a moderator
|
|
|