dbTalk Databases Forums  

question on using merge to upsert

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss question on using merge to upsert in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
mh@pixar.com
 
Posts: n/a

Default Re: question on using merge to upsert - 10-27-2008 , 02:18 AM






DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
These were useful once I got the clue from googling around that I
could use DUAL. Many thanks for the most excellent Morgan's Library!

Here's a cleaned-up and possibly more understandable example...
If you think it's useful, feel free to use this on the MERGE page.

Thanks!
Mark


-- Using MERGE to conditionally update or insert into a table.

create table counters(name varchar2(10), value number);
create or replace procedure increment_counter(zname varchar2)
as
begin
-- if the row already exists, update it
-- else create a new row
merge into counters using dual on (name = zname)
when matched then update value=value + 1
when not matched then insert (name, value) values (zname, 1);
end;
/

call increment_counter('foo');
call increment_counter('foo');
call increment_counter('bar');
select * from counters;

--
Mark Harrison
Pixar Animation Studios


Reply With Quote
  #12  
Old   
mh@pixar.com
 
Posts: n/a

Default Re: question on using merge to upsert - 10-27-2008 , 02:18 AM






DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
These were useful once I got the clue from googling around that I
could use DUAL. Many thanks for the most excellent Morgan's Library!

Here's a cleaned-up and possibly more understandable example...
If you think it's useful, feel free to use this on the MERGE page.

Thanks!
Mark


-- Using MERGE to conditionally update or insert into a table.

create table counters(name varchar2(10), value number);
create or replace procedure increment_counter(zname varchar2)
as
begin
-- if the row already exists, update it
-- else create a new row
merge into counters using dual on (name = zname)
when matched then update value=value + 1
when not matched then insert (name, value) values (zname, 1);
end;
/

call increment_counter('foo');
call increment_counter('foo');
call increment_counter('bar');
select * from counters;

--
Mark Harrison
Pixar Animation Studios


Reply With Quote
  #13  
Old   
mh@pixar.com
 
Posts: n/a

Default Re: question on using merge to upsert - 10-27-2008 , 02:18 AM



DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
These were useful once I got the clue from googling around that I
could use DUAL. Many thanks for the most excellent Morgan's Library!

Here's a cleaned-up and possibly more understandable example...
If you think it's useful, feel free to use this on the MERGE page.

Thanks!
Mark


-- Using MERGE to conditionally update or insert into a table.

create table counters(name varchar2(10), value number);
create or replace procedure increment_counter(zname varchar2)
as
begin
-- if the row already exists, update it
-- else create a new row
merge into counters using dual on (name = zname)
when matched then update value=value + 1
when not matched then insert (name, value) values (zname, 1);
end;
/

call increment_counter('foo');
call increment_counter('foo');
call increment_counter('bar');
select * from counters;

--
Mark Harrison
Pixar Animation Studios


Reply With Quote
Reply




Thread Tools
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 On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.