Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Upsert

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-25-03, 11:02
kukuk kukuk is offline
Member
 
Join Date: Apr 2003
Location: Israel
Posts: 81
Upsert

Hi,

Where can I learn about UPSERT command ?
Tx.
__________________
--
kukuk
Reply With Quote
  #2 (permalink)  
Old 05-25-03, 12:11
billm billm is offline
Drunkard
 
Join Date: Nov 2002
Location: Desk, slightly south of keyboard
Posts: 695
Hi kukuk,

In the old (!) days you might write

begin
update <table> set <column> = <value> where <primarykey> = <key>;
if sql%rowcount = 0 then
insert into <table> (<column>,<column>) values (<key>,<value>);
end if;
end;

Ie, update an existing row, but if it doesn't exist, insert one.

Some db's have now introduced various syntaxes for doing it directly, ie upsert, merge etc. I believe 9i has such a beast, and would suggest the 9i docs, try otn.oracle.com

Hth
Bill
Reply With Quote
  #3 (permalink)  
Old 05-25-03, 13:01
kukuk kukuk is offline
Member
 
Join Date: Apr 2003
Location: Israel
Posts: 81
Thank you, billm.

I want to know what happens with triggers when I run this <upsert>.
Let's say, my <upsert> updates 3 rows and inserts another 2.
Let's say, I have different triggers after insert and after update.
Will both of them fire ?
__________________
--
kukuk
Reply With Quote
  #4 (permalink)  
Old 05-25-03, 13:10
billm billm is offline
Drunkard
 
Join Date: Nov 2002
Location: Desk, slightly south of keyboard
Posts: 695
Hi Kukuk,

To be honest I don't know for certain.

I would imagine that with FOR EACH ROW triggers that the correct trigger would fire, otherwise the trigger code would be illegal. Ie, in an after insert trigger, there are no :OLD values, and referencing them would cause an error. So certainly for these types of triggers I would imagine they have to fire correctly.

For triggers fired after the whole operation, I'm not sure but I would think it isn't quite as important as with "for each row" triggers, as in a traditional bulk insert/update trigger you don't know which specific rows were touched anyway?

The only thing I can suggest is to create a couple and find out? This begs the question is there a new trigger firing type called "after upsert on..." :-)

Hth
Bill
Reply With Quote
  #5 (permalink)  
Old 05-25-03, 13:16
kukuk kukuk is offline
Member
 
Join Date: Apr 2003
Location: Israel
Posts: 81
Thumbs up

Thanks, Bill
__________________
--
kukuk
Reply With Quote
  #6 (permalink)  
Old 05-26-03, 13:57
clio_usa clio_usa is offline
Registered User
 
Join Date: Apr 2002
Location: California, USA
Posts: 482
Thumbs up

Here is an example:

MERGE INTO sales s
USING new_sales n
ON (s.sales_transaction_id = n.sales_transaction_id)
WHEN MATCHED THEN
UPDATE s_quantity = s_quantity + n_quantity, s_dollar = s_dollar + n_dollar
WHEN NOT MATCHED THEN
INSERT (sales_quantity_sold, sales_dollar_amount)
VALUES (n.sales_quantity_sold, n.sales_dollar_amount);


For more information follow this link Implementing an Efficient MERGE Operation


Hope that helps,

clio_usa
OCP - DBA
Visit our Web Site
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

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