Why not use a sequence to populate ID, and let it fire of a before insert trigger. code example below:
 
 
create sequence TAB1_PKSEQ ;
create or replace trigger test_pkgen
BEFORE INSERT OR UPDATE OF col_id on TABLE_A
FOR EACH ROW
BEGIN
IF INSERTING THEN
  SELECT TAB1_PKSEQ1.NextVal INTO :new.COL_ID FROM DUAL;
  /* or something like the above */
END IF;
END;
/
I am assuming you have other columns to populate too, which is not the PK of the table.
 
Cheers :

Ferenc Mantfeld
Dreaming costs you nothing. Not dreaming costs you everything.
----- Original Message -----
Sent: Tuesday, February 25, 2003 6:49 AM
Subject: SQL question

OS: Solaris 2.8

Database: Oracle 9.2.0.2

 

Situation in chronological order

Connection A: select * from table A where id = 1;  Result: no rows returned  -- This means I need to insert the row, as it does not exists yet.

Connection B: select * from table A where id = 1;  Result: no rows returned  -- This means I need to insert the row, as it does not exists yet.

Connection A: insert into table A(id) values = 1;  Result: 1 row inserted

Connection B: insert into table A(id) values = 1;  Result: Unique constraint violated  --  This is the problem.  How do I avoid this happening?

 

Question:  How can I force connection B to wait for connection A to insert the new row before it does the select?

 

If I were updating the row, I could use the "for update" clause to force the wait.  Is there a clean way to do that for an insert?

 

Thanks for your help,

 

Rick Stephenson

 



This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to which they are addressed. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, forwarding or otherwise distributing or taking any action in reliance on the contents of this information is strictly prohibited.



Reply via email to