If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > MERGE statement with "with" clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-07, 17:34
db2Aishani db2Aishani is offline
Registered User
 
Join Date: Aug 2007
Posts: 3
MERGE statement with "with" clause

Hi I am having problem while using with clause in following syntax:

MERGE INTO tablename tr
USING ( with WTab as (
select distinct xxx as ID1, yyy as ID2 from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab
) KeyList
ON tr.Key=KeyList.Key
When NOT MATCHED THEN
INSERT ......

We get the syntax error saying "as following with Wtab is not valid"
where as when I remove the "with clause" and use the same table outside then it works fine.

So Is there any other systax I should be following or whether this MERGE statement can not work with "With" Clause?

Any help is appreciated.
Reply With Quote
  #2 (permalink)  
Old 08-20-07, 08:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by db2Aishani
Hi I am having problem while using with clause in following syntax:

MERGE INTO tablename tr
USING ( with WTab as (
select distinct xxx as ID1, yyy as ID2 from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab
) KeyList
ON tr.Key=KeyList.Key
When NOT MATCHED THEN
INSERT ......

We get the syntax error saying "as following with Wtab is not valid"
where as when I remove the "with clause" and use the same table outside then it works fine.

So Is there any other systax I should be following or whether this MERGE statement can not work with "With" Clause?

Any help is appreciated.

I do not know if it will fix the entire problem, but the Common Table Expression (CTE) should look like this:

with WTab (ID1, ID2) as (
select distinct xxx, yyy from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab


Andy
Reply With Quote
  #3 (permalink)  
Old 08-20-07, 08:36
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The MERGE statement expects a "fullselect". And a CTE is not a fullselect but rather part of a SELECT statement only. Thus, the above won't work and you should simply use traditional subselects.

http://publib.boulder.ibm.com/infoce...c/r0000877.htm
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 08-20-07, 12:06
db2Aishani db2Aishani is offline
Registered User
 
Join Date: Aug 2007
Posts: 3
Quote:
Originally Posted by ARWinner
I do not know if it will fix the entire problem, but the Common Table Expression (CTE) should look like this:

with WTab (ID1, ID2) as (
select distinct xxx, yyy from table2
)
select ID1 as key from WTab
Union all
select ID2 as key from WTab


Andy
Thanks Andy for your reply,
I tried doing that too but could not work out.

Thanks,
Reply With Quote
  #5 (permalink)  
Old 08-20-07, 12:10
db2Aishani db2Aishani is offline
Registered User
 
Join Date: Aug 2007
Posts: 3
Quote:
Originally Posted by stolze
The MERGE statement expects a "fullselect". And a CTE is not a fullselect but rather part of a SELECT statement only. Thus, the above won't work and you should simply use traditional subselects.

http://publib.boulder.ibm.com/infoce...c/r0000877.htm

Thanks Mr. Knut Stolze,
Thanks for clearing my doubt, I understand that why it was not working it.

Do you thing If I create view and use the view instead or the CTE,
I know that this will again same as executing the distinct select twice.

I just thought to check your suggestions!

Thanks for your reply.
Reply With Quote
  #6 (permalink)  
Old 08-20-07, 15:40
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you worry about running a DISTINCT twice, you should first check the plan that this is really happening.

Besides, your query above is definitively not seminatically equivalent to:
Code:
SELECT DISTINCT id1 FROM ...
UNION ALL
SELECT DISTINCT id2 FROM ...
Your query removes duplicate pairs of id1, id2.

Maybe we (you) should first figure out what exactly you want to do and then write the query. Optimizing it should be done afterwards.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 08-24-07, 11:01
dpressm dpressm is offline
Registered User
 
Join Date: Aug 2007
Posts: 1
From a colleague of the poster

Thanks you all for your help.

We certainly agree with your comments about "running the DISTINCT twice"

We simplified the code for the posting.

Table2 is extraordinarily large. The id1 and id2 are not indexed (this query is run once per update of table_2) and have combined very few combinations.

By doing it this way the query runs in minutes insead of an hour.

and infact we have id1,id2,id3,id4.

So if you have a better way of getting distinct lists of four unindexed columns please let us know.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On