DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old May 10th, 2008, 11:02 PM
jaffer123 jaffer123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 jaffer123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 51 sec
Reputation Power: 0
DB2 Merge

Hello All,

I'm have 2 tables Tb1 and Tb2.
Tb1 columns:
C1, C2, C3, C4, C5, C6, C7
k1 A, 1, 2, B, 3, 4

Tb2 columns:
C1, C2, C3, C4
K1, A, 1, 2
K1, B, 3, 4

Tb2 is pivoted. So requirement is i want to update from Tb1 if the key values are available in Tb2 and want to insert if the key values are not available in Tb2.

For that i'm using Merge: I can able to do update but insert is failing.

Could anybody pl help me here.

MERGE INTO Tb1 AS a
USING (SELECT * FROM Tb2) AS b ON (a.c1 = b.c1) AND a.c2 IN (b.c2, b.c5)
WHEN MATCHED AND (a.c2 = b.c2) THEN
UPDATE SET (a.c3,a.c4) = (b.c3,b.c4)

WHEN MATCHED AND (a.c2 = b.c5) THEN
UPDATE SET (a.c3,a.c4) = (b.c6,b.c7)

WHEN NOT MATCHED THEN
#Here i'm spliting Tb1 record into 2 records to insert into Tb2
INSERT SELECT c1, c2, c3, c4 FROM Tb1 WHERE c1=b.c1
UNION ALL
SELECT c1, c5, c6, c7 FROM Tb1 WHERE c1=b.c1;

But in Db2 we can do multiple inserts with single Insert statement like below. The same statement is not working in Merge s

INSERT into Tb2
SELECT c1, c2, c3, c4 FROM Tb1 WHERE c1='K1' AND c2='A'
UNION ALL
SELECT c1, c5, c6, c7 FROM Tb1 WHERE c1='K1' AND c5='B';

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > DB2 Merge


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway