472,353 Members | 1,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

"Merge Into ..." Trouble

I'm going crazy trying to write a working merge-statement. Why doesn't
the following work:

MERGE INTO utSystem.t_Filter_Results AS Res
USING (SELECT 9 as clubid, 50 as filterid, ID as MemberID
FROM club.vc_9
WHERE (("LOCATION#" = 'Zürich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS
NULL))
) AS Fil
ON Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID

WHEN NOT MATCHED AND FIl.MemberID IS NULL THEN
DELETE

WHEN NOT MATCHED AND FIl.MemberID IS NOT NULL THEN
INSERT (ClubID, FilterID, MemberID)
VALUES (Fil.ClubID, Fil.FilterID, Fil.MemberID)

ELSE IGNORE
Error returned is: SQL0104N Auf "emberID IS NULL THEN" folgte das
unerwartete Token "DELETE". Zu den möglichen Token gehören:
"<merge_when_spec_body2>". SQLSTATE=42601. Sorry for German message.
It says that the token is not allowed in the context.

Also if I try replacing "AND Fil.MemberID IS NOT NULL" by an expression
using Res.MemberID it also says that its not allowed in the context.

Is the problem that if I filter by "NOT MATCHED" then there is no
cursor on a column to delte?

Regards
Janick

Nov 12 '05 #1
5 3208
Janick Bernet wrote:
I'm going crazy trying to write a working merge-statement. Why doesn't
the following work:

MERGE INTO utSystem.t_Filter_Results AS Res
USING (SELECT 9 as clubid, 50 as filterid, ID as MemberID
FROM club.vc_9
WHERE (("LOCATION#" = 'Zürich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS
NULL))
) AS Fil
ON Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID

WHEN NOT MATCHED AND FIl.MemberID IS NULL THEN
DELETE

WHEN NOT MATCHED AND FIl.MemberID IS NOT NULL THEN
INSERT (ClubID, FilterID, MemberID)
VALUES (Fil.ClubID, Fil.FilterID, Fil.MemberID)

ELSE IGNORE
Error returned is: SQL0104N Auf "emberID IS NULL THEN" folgte das
unerwartete Token "DELETE". Zu den möglichen Token gehören:
"<merge_when_spec_body2>". SQLSTATE=42601. Sorry for German message.
It says that the token is not allowed in the context.


I think you have a problem with the DELETE. The documentation says:

"Rows in the target that match the source can be deleted or updated as
specified, and rows that do not exist in the target can be inserted."

You have the case "NOT MATCHED", so you can only use INSERT as the
<modification-operation>, I'd say. After all, what is DB2 supposed to
delete if there is nothing that matched?

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #2
Knut Stolze wrote:
Janick Bernet wrote:

I'm going crazy trying to write a working merge-statement. Why doesn't
the following work:

MERGE INTO utSystem.t_Filter_Results AS Res
USING (SELECT 9 as clubid, 50 as filterid, ID as MemberID
FROM club.vc_9
WHERE (("LOCATION#" = 'Zürich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS
NULL))
) AS Fil
ON Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID

WHEN NOT MATCHED AND FIl.MemberID IS NULL THEN
DELETE

WHEN NOT MATCHED AND FIl.MemberID IS NOT NULL THEN
INSERT (ClubID, FilterID, MemberID)
VALUES (Fil.ClubID, Fil.FilterID, Fil.MemberID)

ELSE IGNORE
Error returned is: SQL0104N Auf "emberID IS NULL THEN" folgte das
unerwartete Token "DELETE". Zu den möglichen Token gehören:
"<merge_when_spec_body2>". SQLSTATE=42601. Sorry for German message.
It says that the token is not allowed in the context.

I think you have a problem with the DELETE. The documentation says:

"Rows in the target that match the source can be deleted or updated as
specified, and rows that do not exist in the target can be inserted."

You have the case "NOT MATCHED", so you can only use INSERT as the
<modification-operation>, I'd say. After all, what is DB2 supposed to
delete if there is nothing that matched?

Correct, try this:
MERGE INTO utSystem.t_Filter_Results AS Res
USING (SELECT 9 as clubid, 50 as filterid, ID as MemberID
FROM club.vc_9
WHERE (("LOCATION#" = 'Zürich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS
NULL))
) AS Fil
ON Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID

WHEN MATCHED THEN DELETE
WHEN NOT MATCHED THEN
INSERT (ClubID, FilterID, MemberID)
VALUES (Fil.ClubID, Fil.FilterID, Fil.MemberID)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
Knut, Serge thanks for you answers. If Knut is right, merge will not be
able to do what I want.

The Idea is to only insert rows which are in the table "Fil", to delete
rows which are not in the table "Fil" and to leave rows which are in
both tables untouched. So Serges solution does work, but doesn't do
what I want to :) Can I do with merge what I intended or is it not
suited for this? Is there another solution, better than delete all old
rows and then insert the new ones?

Regards
Janick

Nov 12 '05 #4
In my understanding, you want to do........
From these two sample data

------------------------- Commands Entered -------------------------
SELECT * FROM utSystem.t_Filter_Results
ORDER BY 1;
--------------------------------------------------------------------

CLUBID FILTERID MEMBERID
----------- ----------- -----------
100 50 1
200 50 2
300 50 3
400 40 3

4 record(s) selected.
------------------------- Commands Entered -------------------------
SELECT * FROM club.vc_9;
--------------------------------------------------------------------

ID LOCATION# ENTERING_DATE LEAVING_DATE
----------- --------- ------------- ------------
1 Z?rich 2005-10-19 -
2 ZXrich 2005-10-19 -
3 Z?rich 2005-10-14 -
4 Z?rich 2005-10-29 2005-11-03

4 record(s) selected.

Get following result
------------------------- Commands Entered -------------------------
SELECT * FROM utSystem.t_Filter_Results;
--------------------------------------------------------------------

CLUBID FILTERID MEMBERID
----------- ----------- -----------
300 50 3
9 50 4
100 50 1

3 record(s) selected.

If so, one idea is DELETE and INSERT separately:
DELETE FROM utSystem.t_Filter_Results Res
WHERE NOT EXISTS
(SELECT *
FROM club.vc_9 Vc9
WHERE ((LOCATION# = 'Z?rich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS
NULL))
AND Res.MemberID = Vc9.ID
AND Res.FilterID = 50
);

INSERT INTO utSystem.t_Filter_Results
WITH Fil AS (
SELECT 9 as clubid, 50 as filterid, ID as MemberID
FROM club.vc_9 Vc9
WHERE ((LOCATION# = 'Z?rich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE IS NULL))
)
SELECT Fil.ClubID, Fil.FilterID, Fil.MemberID
FROM Fil
WHERE NOT EXISTS
(SELECT *
FROM utSystem.t_Filter_Results Res
WHERE Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID
);

My another idea using MERGE is something complicated and I suspect
performance. But, I'll show you it for your reference:
MERGE INTO utSystem.t_Filter_Results AS Res
USING (SELECT 9 as clubid, COALESCE(R.FilterID, 50) as filterid
, COALESCE(R.MemberID, V.ID) as MemberID
, CASE
WHEN V.ID IS NULL THEN
2
WHEN R.MemberID IS NULL
AND R.FilterID IS NULL THEN
1
ELSE 0
END AS Matched
FROM (SELECT *
FROM club.vc_9
WHERE ((LOCATION# = 'Z?rich'))
AND (ENTERING_DATE <= CURRENT DATE)
AND ((LEAVING_DATE > CURRENT DATE) OR (LEAVING_DATE
IS NULL))
) AS V
FULL OUTER JOIN
utSystem.t_Filter_Results AS R
ON R.MemberID = V.ID
AND R.FilterID = 50
) AS Fil
ON Res.MemberID = Fil.MemberID
AND Res.FilterID = Fil.FilterID
AND 2 = Fil.Matched
WHEN MATCHED THEN
DELETE
WHEN NOT MATCHED AND 1 = Fil.Matched THEN
INSERT (ClubID, FilterID, MemberID)
VALUES (Fil.ClubID, Fil.FilterID, Fil.MemberID)
;

Nov 12 '05 #5
Tonkuma, thanks a lot for your detailed answer. I think I will go with
the first approach, which should be the faster and more readable one :)

Regards
Janick

Nov 12 '05 #6

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

Similar topics

1
by: Sean Dettrick | last post by:
Hi, I have several <vector>'s of the same length, with entries as follows: I= A= B= I want to use STL to make (I == 0) a mask to operate on...
3
by: anurag | last post by:
Hi, Is "MERGE INTO..." statement allowed to be executed in a dynamic SQL statement, inside an SQL Stored Procedure? I am using DB2 UDB 8.1.2 on...
1
by: mcmreddy | last post by:
Hello, Is there any advantage of using "MERGE INTO" SQL instead of using "EXISTS" AND "NOT EXISTS" in DB2?. Thanks, --Chandra
2
by: Lee | last post by:
I have a Word template that is set to merge with our client database. After I run the merge, I merge to a new document, and then save the new...
0
by: Sheila | last post by:
I have set up a Word mail merge document of the type Directory which allows me to display all of the records on a single page rather than putting...
3
by: MirzaSila | last post by:
I am using mail merge option in msWord for creating labels, data source is msAccess. Problem is when I choose option "merge to printer" it only...
2
by: john sun | last post by:
Hi, Dear gurus, In the web deployment project, there is one option called "Output Assemblies page". The first choice is "Merge all outputs to...
0
by: Quisbamsis | last post by:
Access Database Merge to Publisher or Word. Everytime I run a "Mail and Catalogue Merge Wizard" in Publisher or Word and I get to the "Mail...
0
by: jeoffh | last post by:
Background: I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.