If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Help on Stored Procedures

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-20-04, 02:47
lloydnwo lloydnwo is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 262
Help on Stored Procedures

Dear All,

I need some help on procedures. I have a table which has around 5000 records. I need to insert additional 200 records, i have insert statements ready for the same. I need to make sure while inserting that if records does not exists in the table it should insert them or else update them, there is a unique constraint on the id. If records exists it throws me a unique constraint violation error. How can i do it with the help of procedure. Please advice.

Best Regards,

Lloyd
Reply With Quote
  #2 (permalink)  
Old 07-21-04, 11:03
kueheri kueheri is offline
Registered User
 
Join Date: Jul 2004
Posts: 8
What about creating a temp table with the same structure, loading the records to the temp table, then doing an insert for those that don't exist in the original?

CREATE TEMP TABLE tmptbl (
field1 char(1)
) WITH NO LOG;

INSERT INTO tmptbl <--- 200 records


INSERT INTO origtbl
SELECT * FROM tmptable WHERE id NOT IN (SELECT id FROM origtbl)

I'm sure there's better ways to do it, but this is the way I've always done it...

<edit post>

Forgot to address your updates... you could either do this before or after the inserts... if you do this before, it would have less work to do since it wouldn't be running against the records you just inserted...

UPDATE origtbl
SET (field1, field2, ..., fieldn) = (SELECT field1, field2, ..., fieldn FROM tmptbl
WHERE tmptbl.id = origtbl.id)
WHERE id IN (SELECT id FROM tmptbl)

slow & clunky but works, from what I remember...



Quote:
Originally Posted by lloydnwo
Dear All,

I need some help on procedures. I have a table which has around 5000 records. I need to insert additional 200 records, i have insert statements ready for the same. I need to make sure while inserting that if records does not exists in the table it should insert them or else update them, there is a unique constraint on the id. If records exists it throws me a unique constraint violation error. How can i do it with the help of procedure. Please advice.

Best Regards,

Lloyd

Last edited by kueheri; 07-21-04 at 11:08. Reason: additional info
Reply With Quote
  #3 (permalink)  
Old 07-21-04, 23:51
lloydnwo lloydnwo is offline
Registered User
 
Join Date: Aug 2003
Location: India
Posts: 262
Hi Kueheri,

Thanks for your feedback, i'll work on it.

Regards,

Lloyd
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On