dbTalk Databases Forums  

Db2 merge from jdbc with provided values

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Db2 merge from jdbc with provided values in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
phil
 
Posts: n/a

Default Db2 merge from jdbc with provided values - 02-15-2012 , 07:53 AM






Hi all!
I would like to use the db2 merge statement submitting it as a
statement from jdbc. I am in the following scenario. I'm working with
a proprietary persistence layer and I'm handling an entity I don't
know whether it's already persisted or not and I would like to use the
merge statement in order to insert or update a row on the database. Is
it possible? Suppose I'm working with the table people with three
columns: id, name, surname and I'm handling an entity with id="5",
name="chuck", surname="norris" Am I able to issue:

MERGE INTO people AS t
USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s
ON (t.id = s.id)
WHEN MATCHED THEN
UPDATE SET t.name=s.name, t.surmane=s.surname
WHEN NOT MATCHED THEN
INSERT
(id, name, surname)
VALUES (s.id, s.name, s.surname)

such a statement? I'm trying to do that but I got an error. I don't
think it's allowed to use a select after USING:

USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s

I also tryed to do:

USING VALUES('5','chuck','norris') AS s(id,chuck,norris)

but maybe I was wrong with the syntax and it dosn't work. Any help
would be appreciated.
Thanks Fil

Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: Db2 merge from jdbc with provided values - 02-15-2012 , 09:06 AM






What error message(s) did you got? What were exact error code and
text?

What are your DB2 version/release and platform OS?

Reply With Quote
  #3  
Old   
Sathyaram Sannasi
 
Posts: n/a

Default Re: Db2 merge from jdbc with provided values - 02-15-2012 , 10:29 AM



On Feb 15, 1:53*pm, phil <filippoma... (AT) gmail (DOT) com> wrote:
Quote:
Hi all!
I would like to use the db2 merge statement submitting it as a
statement from jdbc. I am in the following scenario. I'm working with
a proprietary persistence layer and I'm handling an entity I don't
know whether it's already persisted or not and I would like to use the
merge statement in order to insert or update a row on the database. Is
it possible? Suppose I'm working with the table people with three
columns: id, name, surname and I'm handling an entity with id="5",
name="chuck", surname="norris" Am I able to issue:

MERGE INTO people AS t
* USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s
* * ON (t.id = s.id)
* WHEN MATCHED THEN
* * * UPDATE SET t.name=s.name, t.surmane=s.surname
* WHEN NOT MATCHED THEN
* * INSERT
* * * (id, name, surname)
* * VALUES (s.id, s.name, s.surname)

such a statement? I'm trying to do that but I got an error. I don't
think it's allowed to use a select after USING:

USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s

I also tryed to do:

USING VALUES('5','chuck','norris') AS s(id,chuck,norris)

but maybe I was wrong with the syntax and it dosn't work. Any help
would be appreciated.
Thanks Fil
Created a table and copy-pasted your SQL .. Worked after correcting
a typo in your query -- "surmane"


create table people(id int,name char(10),surname char(10))
DB20000I The SQL command completed successfully.

MERGE INTO people AS t USING (select '5' as id, 'chuck' as name,
'norris' as surname from SYSIBM.SYSDUMMY1)As s ON (t.id = s.id) WHEN
MATCHED THEN UPDATE SET t.name=s.name, t.surname=s.surname WHEN NOT
MATCHED THEN INSERT (id, name, surname) VALUES (s.id, s.name,
s.surname)
DB20000I The SQL command completed successfully.

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Db2 merge from jdbc with provided values - 02-15-2012 , 04:34 PM



On 2012-02-15 14:53, phil wrote:

[...]

Quote:
MERGE INTO people AS t
USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s
ON (t.id = s.id)
WHEN MATCHED THEN
UPDATE SET t.name=s.name, t.surmane=s.surname
WHEN NOT MATCHED THEN
INSERT
(id, name, surname)
VALUES (s.id, s.name, s.surname)

such a statement? I'm trying to do that but I got an error. I don't
think it's allowed to use a select after USING:
That is allowed, what error did you get?

Quote:
USING (select '5' as id, 'chuck' as name, 'norris' as surname from
SYSIBM.SYSDUMMY1)As s

I also tryed to do:

USING VALUES('5','chuck','norris') AS s(id,chuck,norris)

Should also work, but you should probably name your columns otherwise

USING VALUES('5','chuck','norris') AS s(id,name,surname)


/Lennart

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 - 2013, Jelsoft Enterprises Ltd.