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
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default question on using merge to upsert - 10-25-2008 , 08:13 PM






I would like to use MERGE to upsert some simple values.
Here's what I've got, which works fine with all my tests,
but MERGE is pretty complicated and I want to make sure
I've got my tiny brain around the problem.

Is this a reasonable way to do this? Is there a better way?

Many TIA!
Mark

create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;

A B
---------------------- ----------------------
10 2
20 1

--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
DA Morgan
 
Posts: n/a

Default Re: question on using merge to upsert - 10-26-2008 , 08:06 AM






mh (AT) pixar (DOT) com wrote:
Quote:
I would like to use MERGE to upsert some simple values.
Here's what I've got, which works fine with all my tests,
but MERGE is pretty complicated and I want to make sure
I've got my tiny brain around the problem.

Is this a reasonable way to do this? Is there a better way?

Many TIA!
Mark

create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;

A B
---------------------- ----------------------
10 2
20 1
Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?

I,forone, am sitting here looking at "using dual" and wondering
what you are actually trying to accomplish that requires merge.

There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #3  
Old   
DA Morgan
 
Posts: n/a

Default Re: question on using merge to upsert - 10-26-2008 , 08:06 AM



mh (AT) pixar (DOT) com wrote:
Quote:
I would like to use MERGE to upsert some simple values.
Here's what I've got, which works fine with all my tests,
but MERGE is pretty complicated and I want to make sure
I've got my tiny brain around the problem.

Is this a reasonable way to do this? Is there a better way?

Many TIA!
Mark

create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;

A B
---------------------- ----------------------
10 2
20 1
Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?

I,forone, am sitting here looking at "using dual" and wondering
what you are actually trying to accomplish that requires merge.

There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #4  
Old   
DA Morgan
 
Posts: n/a

Default Re: question on using merge to upsert - 10-26-2008 , 08:06 AM



mh (AT) pixar (DOT) com wrote:
Quote:
I would like to use MERGE to upsert some simple values.
Here's what I've got, which works fine with all my tests,
but MERGE is pretty complicated and I want to make sure
I've got my tiny brain around the problem.

Is this a reasonable way to do this? Is there a better way?

Many TIA!
Mark

create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;

A B
---------------------- ----------------------
10 2
20 1
Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?

I,forone, am sitting here looking at "using dual" and wondering
what you are actually trying to accomplish that requires merge.

There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #5  
Old   
DA Morgan
 
Posts: n/a

Default Re: question on using merge to upsert - 10-26-2008 , 08:06 AM



mh (AT) pixar (DOT) com wrote:
Quote:
I would like to use MERGE to upsert some simple values.
Here's what I've got, which works fine with all my tests,
but MERGE is pretty complicated and I want to make sure
I've got my tiny brain around the problem.

Is this a reasonable way to do this? Is there a better way?

Many TIA!
Mark

create or replace
procedure ups(xa number)
as
begin
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;
end ups;
/
drop table mergetest;
create table mergetest(a number, b number);
call ups(10);
call ups(10);
call ups(20);
select * from mergetest;

A B
---------------------- ----------------------
10 2
20 1
Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?

I,forone, am sitting here looking at "using dual" and wondering
what you are actually trying to accomplish that requires merge.

There are working demos of MERGE in Morgan's Library at www.psoug.org
that may help you understand how it works.
--
Daniel A. Morgan
Oracle Ace Director & Instructor
University of Washington
damorgan@x.washington.edu (replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


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

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



DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;

A B
---------------------- ----------------------
10 2
20 1

Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?
Suppose I have a table of counters keyed by a name:

name | value
-------+-------
foo | 12
bar | 45

and a stored procedure:

bump(name varchar2)

The logic is:

if the name exists in the table, add one to the value
if the name does not exist in the table, insert a row with the name
and set value = 1.

So far, the clearest expression of this I've found seems to
be in mysql, which would be something like this

insert into counters(name,value) values('foo',1)
on duplicate key update value=value+1;

I mentioned DUAL because MERGE is not the obvious choice if you
are working with one table, and it's a bit of a hack (in the
good sense of the word) to use DUAL as the second table.

Thanks!
Mark.

--
Mark Harrison
Pixar Animation Studios


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

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



DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;

A B
---------------------- ----------------------
10 2
20 1

Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?
Suppose I have a table of counters keyed by a name:

name | value
-------+-------
foo | 12
bar | 45

and a stored procedure:

bump(name varchar2)

The logic is:

if the name exists in the table, add one to the value
if the name does not exist in the table, insert a row with the name
and set value = 1.

So far, the clearest expression of this I've found seems to
be in mysql, which would be something like this

insert into counters(name,value) values('foo',1)
on duplicate key update value=value+1;

I mentioned DUAL because MERGE is not the obvious choice if you
are working with one table, and it's a bit of a hack (in the
good sense of the word) to use DUAL as the second table.

Thanks!
Mark.

--
Mark Harrison
Pixar Animation Studios


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

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



DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;

A B
---------------------- ----------------------
10 2
20 1

Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?
Suppose I have a table of counters keyed by a name:

name | value
-------+-------
foo | 12
bar | 45

and a stored procedure:

bump(name varchar2)

The logic is:

if the name exists in the table, add one to the value
if the name does not exist in the table, insert a row with the name
and set value = 1.

So far, the clearest expression of this I've found seems to
be in mysql, which would be something like this

insert into counters(name,value) values('foo',1)
on duplicate key update value=value+1;

I mentioned DUAL because MERGE is not the obvious choice if you
are working with one table, and it's a bit of a hack (in the
good sense of the word) to use DUAL as the second table.

Thanks!
Mark.

--
Mark Harrison
Pixar Animation Studios


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

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



DA Morgan <damorgan (AT) psoug (DOT) org> wrote:
Quote:
merge into mergetest m using dual on (a = xa)
when not matched then insert (a,b) values (xa,1)
when matched then update set b = b+1;

A B
---------------------- ----------------------
10 2
20 1

Without you stating the business rules how can we know if what
your statement is doing corresponds with your goal?
Suppose I have a table of counters keyed by a name:

name | value
-------+-------
foo | 12
bar | 45

and a stored procedure:

bump(name varchar2)

The logic is:

if the name exists in the table, add one to the value
if the name does not exist in the table, insert a row with the name
and set value = 1.

So far, the clearest expression of this I've found seems to
be in mysql, which would be something like this

insert into counters(name,value) values('foo',1)
on duplicate key update value=value+1;

I mentioned DUAL because MERGE is not the obvious choice if you
are working with one table, and it's a bit of a hack (in the
good sense of the word) to use DUAL as the second table.

Thanks!
Mark.

--
Mark Harrison
Pixar Animation Studios


Reply With Quote
  #10  
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.