473,388 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Merge statement in db2

I want to trap errors (RI errors), if any, that might turn up during
merge in DB2. Is there some feature like the one in Oracle:

MERGE INTO...
WHEN MATCHED THEN UPDATE...
WHEN NOT MATCHED THEN INSERT...
LOG ERRORS INTO...
I mean is there some LOG ERRORS INTO or an equivalent clause in case
of DB2 merge stmt.

Thanks
amitabh

Jun 6 '07 #1
6 8965
am***********@gmail.com wrote:
I want to trap errors (RI errors), if any, that might turn up during
merge in DB2. Is there some feature like the one in Oracle:

MERGE INTO...
WHEN MATCHED THEN UPDATE...
WHEN NOT MATCHED THEN INSERT...
LOG ERRORS INTO...
I mean is there some LOG ERRORS INTO or an equivalent clause in case
of DB2 merge stmt.
I dont think there is one (and AFAIK it is not defined in the standard
either). Does Oracle have the same feature for insert and update stmts?

/Lennart

Jun 6 '07 #2
On Jun 6, 2:08 pm, Lennart <erik.lennart.jons...@gmail.comwrote:
amitabh.me...@gmail.com wrote:
I want to trap errors (RI errors), if any, that might turn up during
merge in DB2. Is there some feature like the one in Oracle:
MERGE INTO...
WHEN MATCHED THEN UPDATE...
WHEN NOT MATCHED THEN INSERT...
LOG ERRORS INTO...
I mean is there some LOG ERRORS INTO or an equivalent clause in case
of DB2 merge stmt.

I dont think there is one (and AFAIK it is not defined in the standard
either). Does Oracle have the same feature for insert and update stmts?

/Lennart
In oracle the "log errors into.." clause was able to trap the RI
errors and move them to a table i specified. the syntax of that table
is oracle specific.

This is the scenario that i want to do:

I have arnd 10 merge stmts stored in a table. In my stored procedure,
i just fetch these stmts and execute them. Is there some mechanism by
which if there is a RI exception, then that particular merge stmt is
left and control continues with other stmts in the list?

Earlier i was thinking of trapping these errors in diff table (like in
oracle). Since this is not possible, i will be more than happy if the
above mentioned thing can be done.

Jun 6 '07 #3
am***********@gmail.com wrote:
I have around 10 merge stmts stored in a table. In my stored procedure,
i just fetch these stmts and execute them. Is there some mechanism by
which if there is a RI exception, then that particular merge stmt is
left and control continues with other stmts in the list?
Of course. All you need is to catch the RI error (SQLSTATE 23503).

Are all these MERGE statements on different tables? Ideally you would
simply sort them properly.
Or you test the RI condition with the MERGE.
DB2 allows the addition of a predicate to the WHEN [NOT] MATCHED clause.
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 6 '07 #4
On Jun 6, 4:51 pm, Serge Rielau <srie...@ca.ibm.comwrote:
amitabh.me...@gmail.com wrote:
I have around 10 merge stmts stored in a table. In my stored procedure,
i just fetch these stmts and execute them. Is there some mechanism by
which if there is a RI exception, then that particular merge stmt is
left and control continues with other stmts in the list?

Of course. All you need is to catch the RI error (SQLSTATE 23503).

Are all these MERGE statements on different tables? Ideally you would
simply sort them properly.
Or you test the RI condition with the MERGE.
DB2 allows the addition of a predicate to the WHEN [NOT] MATCHED clause.
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Serge

My mistake, i wrote the wrong scenerio... instead of many merge stmt,
there is just one merge stmt.. but many records in a table, out of
which few have RI errors. I want to put all good ones into another
table using this merge stmt. The problem is that when exception occurs
(RI) while merging, none of the record (even good ones) are
transferred. Is there some way that the good records get transferred
inspite of the RI exception gettting thrown?

The for loop I was using is like:

FOR insertSQL AS
(SELECT SQLSTRING, TABLE_NAME FROM my_table)
DO
SET sqlRun = insertSQL.SQLSTRING;
EXECUTE IMMEDIATE sqlRun;
-- COMMIT;

END FOR;
COMMIT;

Jun 6 '07 #5
am***********@gmail.com wrote:
[...]
My mistake, i wrote the wrong scenerio... instead of many merge stmt,
there is just one merge stmt.. but many records in a table, out of
which few have RI errors. I want to put all good ones into another
table using this merge stmt. The problem is that when exception occurs
(RI) while merging, none of the record (even good ones) are
transferred. Is there some way that the good records get transferred
inspite of the RI exception gettting thrown?
I usually handle situations like that by adding predicates in the using
clause that correspond to the constraints that might fail. I.e assuming
a table like

create table T (
t int not null primary key,
u int not null
)

alter table T add constaint C
check (u between 5 and 9)

alter table T add constaint FK
foreign key (u)
references T2 (u)

would mean

merge into T
using ( select * from T3
where u between 5 and 9
and exists (
select 1 from T2
where T3.u = T2.u
) etc
)
when matched ...

Then make a report of the bad ones

select * from T3
where NOT (u between 5 and 9
and exists (
select 1 from T2
where T3.u = T2.u
))

Just a thought

/Lennart

Jun 6 '07 #6
That would be my advise as well.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Robert Stearns | last post by:
I was pointed (thanks to Serge Rielau) to the MERGE statement as the solution to a certain updating problem I had. It works wonderfully. Where is the documentation for this statement!?! I spent...
4
by: marklawford | last post by:
Hi guys, I'm getting the following error message when trying to run a MERGE statement I'm putting together. The syntax looks right to me But i must be missing something. The "srce" table...
5
by: melentina | last post by:
Hello, I'm trying to use MERGE statement in a procedure, please see the code below: procedure gen_nou (p_data date) is v_atribut_id number; begin v_atribut_id=100;
0
by: eonblue20 | last post by:
Hello I\\\'m new to DB2 and I\\\'ve been trying to create the MERGE statement using PreparedStatements in Java as a requirement for a project I\\\'m working on, but I get the following message: ...
6
by: tedlaraghu | last post by:
Hi all, Can someone advice me what's wrong in this merge statement MERGE INTO xxeaccess_client_revenue crev USING (select client_id,client_name,revenue,year_month,year from ...
1
by: UDBDBA | last post by:
Hi, If someone can clarify this, it would be of great help! We have a merge, which is written *only* to update the target table. but the access plan shows branches with inserts and deletes on...
0
by: micksmastamike | last post by:
Hi i am trying to do SCD-2 type logic in a db2 merge statement. That is..update the old record's end_eff_dt as the existing rows max(eff_dt)-1 and update the new record's end_eff_dt to...
7
by: Henry J. | last post by:
I got a dumb question on the merge statement. I read the following example of merge statement at the IBM page:...
0
by: rajkumargnv | last post by:
I know there are differences in MERGE Statement in UDB and z/OS like in z/Os only one 'WHEN MATCHED', one 'WHEN NOT MATCHED' are allowed. My question is more about USING clause, does it always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.