From oracle-l-bounce@freelists.org Wed May 18 13:09:58 2005 Return-Path: Received: from air891.startdedicated.com (root@localhost) by orafaq.com (8.12.10/8.12.10) with ESMTP id j4II9w7Z003157 for ; Wed, 18 May 2005 13:09:58 -0500 X-ClientAddr: 206.53.239.180 Received: from turing.freelists.org (freelists-180.iquest.net [206.53.239.180]) by air891.startdedicated.com (8.12.10/8.12.10) with ESMTP id j4II9w4Z003152 for ; Wed, 18 May 2005 13:09:58 -0500 Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id C18FD194A2F; Wed, 18 May 2005 12:07:08 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 05595-01; Wed, 18 May 2005 12:07:08 -0500 (EST) Received: from turing (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 34EAF194528; Wed, 18 May 2005 12:07:08 -0500 (EST) Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; b=xUtSfGI6pTjFLIQgpibcXCbHZKTsINvoaEsEmRAbbUmp0dusHsyogSNiSkxgLtFFzWPRZDr0GHrRnIERI2W3NID4PgIjarvH9jzEauRTrztOVMR40qiIyaFX8QgxbKBllTFNOdbctue/yuyRf+3gDfeqTlcAhS2eBYM8EsNB7IM= ; Message-ID: <20050518170520.51033.qmail@web52902.mail.yahoo.com> Date: Wed, 18 May 2005 10:05:19 -0700 (PDT) From: Kean Jacinta Subject: Re: TRIGGERS To: oracle-l@freelists.org In-Reply-To: 6667 MIME-Version: 1.0 Content-type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit X-archive-position: 19877 X-ecartis-version: Ecartis v1.0.0 Sender: oracle-l-bounce@freelists.org Errors-To: oracle-l-bounce@freelists.org X-original-sender: jacintakean@yahoo.com Precedence: normal Reply-To: jacintakean@yahoo.com X-list: oracle-l X-Virus-Scanned: by amavisd-new-20030616-p9 (Debian) at avenirtech.net X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on air891.startdedicated.com X-Spam-Status: No, hits=0.7 required=5.0 tests=AWL,FORGED_YAHOO_RCVD, LINES_OF_YELLING,LINES_OF_YELLING_2,UPPERCASE_25_50 autolearn=no version=2.63 HI, the error i get : ORA-0001: unique constraint violated Here is the entire create trigger CREATE OR REPLACE TRIGGER "TESTDB"."MYCLASS_TR" BEFORE INSERT OR UPDATE ON "MYCLASS" FOR EACH ROW DECLARE REFCOUNT NUMBER; DUP_VAL_ON_INDEX EXCEPTION; TEMP NUMBER; BEGIN IF INSERTING THEN LOOP SELECT COUNT(*) INTO REFCOUNT FROM MYCLASS WHERE :NEW.ID = ID; IF REFCOUNT > 0 THEN --RECORD EXISTED IN DATABASE SELECT MYCLASS_SEQ.NEXTVAL INTO TEMP FROM DUAL; END IF; EXIT; --ELSIF REFCOUNT < 0 THEN -- EXIT; -- END IF; END LOOP; IF :NEW.ID IS NULL THEN SELECT MYCLASS_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END IF; END IF; --MAIN IF END; thank JK --- david wendelken wrote: > I don't think you should be getting a unique key > error with the pre-insert row trigger you are > showing. > I think you should be getting a totally different > error. > > Please show the entire create trigger statement, not > just the pl/sql code in it. > > -----Original Message----- > From: Kean Jacinta > Sent: May 18, 2005 9:33 AM > To: oracle-l@freelists.org > Subject: Re: TRIGGERS > > HI, > > here is what i have written in trigger body, i am > really stuck here. help me up , it;s still not > working > . System still complain about the unique key error. > **:( > > > DECLARE > REFCOUNT NUMBER; > DUP_VAL_ON_INDEX EXCEPTION; > TEMP NUMBER; > > BEGIN > IF INSERTING THEN > > LOOP > SELECT COUNT(*) INTO REFCOUNT > FROM myclass > WHERE :NEW.id = id; > > IF REFCOUNT > 0 THEN --RECORD EXISTED IN > DATABASE > SELECT myclass_seq.NEXTVAL INTO TEMP FROM > DUAL; > END IF; > > > END LOOP; > > IF :NEW.ID IS NULL THEN > > > SELECT myclass_SEQ.NEXTVAL > INTO :NEW.ID > FROM DUAL; > END IF; > > END IF; --MAIN IF > > END; > > > Note : have no idea how to use the exception handler > > > > > JKean > > > > --- david wendelken > wrote: > > > If you really want to handle that, you have three > > choices: > > > > 1) Live with it. If you have a program that > > assigns the id values for you, it should be > getting > > its next value from the sequence also. If you do > > that, this just isn't a problem in real life. If > > you aren't willing to ALWAYS get the next id value > > from a sequence, you probably shouldn't be using a > > sequence at all. > > > > 2) Relatively simple, partial solution. Use a > > pre-statement trigger on insert, have it grab the > > next sequence value and verify that the record for > > that sequence number is not already in the table. > > If it is, get the next sequence value until you > find > > an empty one. Assume that all the ones after that > > are ok from then on. Ignore the fact taht you > throw > > away a sequence number each time you do a > statement. > > Remember, I said *partial* solution. > > > > 3) Read up on mutating table errors and autonomous > > transactions. This will take you awhile to read > and > > experiment with. We have time to help with > specific > > problems, but not to write it for you. > > Ask again after you've read that material and are > > are stuck. > > > > > > > > -----Original Message----- > > From: Kean Jacinta > > Sent: May 18, 2005 8:02 AM > > To: david wendelken > > Subject: Re: TRIGGERS > > > > david, > > > > :P oh i see . I am so blur. My concern, > > > > ID > > --- > > 1 <-- autogenerated > > 2 <-- autogenerated > > 3 <-- autogenerated > > 4 <-- manually created > > 5 <-- manually created > > > > My current sequence stay at : 3 , if i issued an > > autotgenerated insert again it will surely prompt > > unique id alrdy existed... or some sort of > database > > error. How can then trigger being smart enough to > > silently generate autoid 6 without raising error ? > > > > I am very close to my objectives alrdy :P > > > > Thank in advance > > Jkean > > > > > > > __________________________________ > Yahoo! Mail Mobile > Take Yahoo! Mail with you! Check email on your > mobile phone. > http://mobile.yahoo.com/learn/mail > -- > http://www.freelists.org/webpage/oracle-l > > -- > http://www.freelists.org/webpage/oracle-l > __________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250 -- http://www.freelists.org/webpage/oracle-l