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 > Update using joins

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-08-11, 08:40
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
Update using joins

Please provide the syntax to update using joins.
I tried many syntax but errors were there.
I also tried MERGE but it also did not work.
Reply With Quote
  #2 (permalink)  
Old 07-08-11, 08:54
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Can you post what SQL you have used and what error you get?
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 07-08-11, 08:57
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
MERGE INTO mbid.TSCHCLD F
USING mbid.TSCH A
ON F.FLIGHT_NUMBER = A.FLIGHT_NUMBER AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
AND F.FLIGHT_DESIGNATOR = A.FLIGHT_DESIGNATOR AND F.FLIGHT_OP_SUFFIX = A.FLIGHT_OP_SUFFIX AND
F.FLIGHT_NUMBER = 24
AND F.FLIGHT_DESIGNATOR = 'DD'
WHEN MATCHED THEN UPDATE SET F.DEI_2 = 'AA';

The Error is-
SQL State = 21506 SQL Code = -788 SQL Message = The same row of target table "MBID.TMSTSCHCLD" was identified more than once for an update, delete or insert operation of the MERGE statement. Exception message = com.ibm.db2.jcc.c.SqlException: The same row of target table "MBID.TMSTSCHCLD" was identified more than once for an update, delete or insert operation of the MERGE statement.

Last edited by pratikp.vasani; 07-08-11 at 08:59. Reason: NA
Reply With Quote
  #4 (permalink)  
Old 07-08-11, 09:33
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Your join condition should be such that for each row selected to be updated in F , there should be only one row in A. As per this error, there is more than one row.

Either you have missed a filter condition or it could be a design flaw or it could be constraint error that has allowed duplicate rows to mbid.TSCH.

If none of these is true, do
Code:
MERGE INTO mbid.TSCHCLD F
USING (select on mbid.TSCH to return only distinct rows for each fltno,deldate,design,suffix) a 
ON F.FLIGHT_NUMBER = A.FLIGHT_NUMBER AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
AND F.FLIGHT_DESIGNATOR = A.FLIGHT_DESIGNATOR AND F.FLIGHT_OP_SUFFIX = A.FLIGHT_OP_SUFFIX AND 
F.FLIGHT_NUMBER = 24 
AND F.FLIGHT_DESIGNATOR = 'DD' 
WHEN MATCHED THEN UPDATE SET F.DEI_2 = 'AA';



You can find the offending row using
Code:
Select F.FLIGHT_NUMBER, F.FIRST_LEG_DEP_DATE, F.FLIGHT_DESIGNATOR,count(*) from mbid.TSCHCLD F, mbid.TSCH A 
Where  
F.FLIGHT_NUMBER = A.FLIGHT_NUMBER AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
AND F.FLIGHT_DESIGNATOR = A.FLIGHT_DESIGNATOR AND F.FLIGHT_OP_SUFFIX = A.FLIGHT_OP_SUFFIX AND 
F.FLIGHT_NUMBER = 24 
AND F.FLIGHT_DESIGNATOR = 'DD' 
FYI, The above query was done on notepad and not checked for syntax or output.

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 07-11-11, 01:45
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
Instead of MERGE, can't we use simple JOINT query.
Also in the Where clause I will be having the conditions which will update multiple rows together.

Last edited by pratikp.vasani; 07-11-11 at 02:10.
Reply With Quote
  #6 (permalink)  
Old 07-11-11, 02:23
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
You have not used column value(s) of A to update F.DEI_2.

So, try
Example (a):
Code:
UPDATE mbid.TSCHCLD F
   SET F.DEI_2 = 'AA'
 WHERE EXISTS
       (SELECT 0
         FROM  mbid.TSCH A
         WHERE F.FLIGHT_NUMBER      = A.FLIGHT_NUMBER
           AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
           AND F.FLIGHT_DESIGNATOR  = A.FLIGHT_DESIGNATOR
           AND F.FLIGHT_OP_SUFFIX   = A.FLIGHT_OP_SUFFIX
           AND F.FLIGHT_NUMBER      = 24 
           AND F.FLIGHT_DESIGNATOR  = 'DD'
       )
or
Example (b):
Code:
UPDATE mbid.TSCHCLD F
   SET F.DEI_2 = 'AA'
 WHERE EXISTS
       (SELECT 0
         FROM  mbid.TSCH A
         WHERE F.FLIGHT_NUMBER      = A.FLIGHT_NUMBER
           AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
           AND F.FLIGHT_DESIGNATOR  = A.FLIGHT_DESIGNATOR
           AND F.FLIGHT_OP_SUFFIX   = A.FLIGHT_OP_SUFFIX
       )
   AND F.FLIGHT_NUMBER      = 24 
   AND F.FLIGHT_DESIGNATOR  = 'DD'
You will get same result by (a) and (b).
And if DB2 optimizer was clever enough, it might choose same access path.
Reply With Quote
  #7 (permalink)  
Old 07-11-11, 02:54
pratikp.vasani pratikp.vasani is offline
Registered User
 
Join Date: Jul 2011
Posts: 16
Hi,
In the above query I am using one more condition that A.ACTION_IDEN <> 'CNL', but it is updating that row also. And the column ACTION_IDEN is not present in the second table.
So is it possible with the above situation.

Last edited by pratikp.vasani; 07-11-11 at 03:27.
Reply With Quote
  #8 (permalink)  
Old 07-11-11, 05:31
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
In the above query I am using one more condition that A.ACTION_IDEN <> 'CNL', but it is updating that row also.
I couldn't understand well your requirements and issue.
If you include the condition(A.ACTION_IDEN <> 'CNL'),
SQL wouldn't update the rows having corresponding row(s) with A.ACTION_IDEN = 'CNL'.
One possibility I could guess was there were multiple corresponding rows in A
but some were A.ACTION_IDEN = 'CNL' and others were A.ACTION_IDEN <> 'CNL'.

Quote:
And the column ACTION_IDEN is not present in the second table.
No problem.

If you want to exclude the rows which have corresponding row(s) in A but all A.ACTION_IDEN = 'CNL',
simply add the condition, like...
Code:
UPDATE mbid.TSCHCLD F
   SET F.DEI_2 = 'AA'
 WHERE EXISTS
       (SELECT 0
         FROM  mbid.TSCH A
         WHERE F.FLIGHT_NUMBER      = A.FLIGHT_NUMBER
           AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
           AND F.FLIGHT_DESIGNATOR  = A.FLIGHT_DESIGNATOR
           AND F.FLIGHT_OP_SUFFIX   = A.FLIGHT_OP_SUFFIX
           AND A.ACTION_IDEN        <> 'CNL'
       )
   AND F.FLIGHT_NUMBER      = 24 
   AND F.FLIGHT_DESIGNATOR  = 'DD'
;
Reply With Quote
  #9 (permalink)  
Old 07-11-11, 20:45
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
If your requirements were
Quote:
Update F if there were matched rows in A,
but not update if some of matched row(s) were A.ACTION_IDEN = 'CNL'.
In other words:
Update F if there were matched rows in A and all of the matched rows were A.ACTION_IDEN <> 'CNL'.
try...
Code:
UPDATE mbid.TSCHCLD F
   SET F.DEI_2 = 'AA'
 WHERE EXISTS
       (SELECT 0
         FROM  mbid.TSCH A
         WHERE F.FLIGHT_NUMBER      = A.FLIGHT_NUMBER
           AND F.FIRST_LEG_DEP_DATE = A.FIRST_LEG_DEP_DATE
           AND F.FLIGHT_DESIGNATOR  = A.FLIGHT_DESIGNATOR
           AND F.FLIGHT_OP_SUFFIX   = A.FLIGHT_OP_SUFFIX
         HAVING
               SUM(CASE A.ACTION_IDEN
                   WHEN 'CNL' THEN 1
                   ELSE            0
                   END
                  ) = 0
       )
   AND F.FLIGHT_NUMBER      = 24 
   AND F.FLIGHT_DESIGNATOR  = 'DD'
;
Note: If your DB2 supports LISTAGG aggregate function, HAVING clause can be replaced by
HAVING LOCATE( 'CNL:' , LISTAGG(a.action_iden || ':') ) = 0
or
HAVING LISTAGG(a.action_iden || ':') NOT LIKE '%CNL:%'

Last edited by tonkuma; 07-11-11 at 22:25. Reason: Add Note.
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