Updating Db2 tables in succession

Ask question on - DataBase Concepts, IBM Mainframe DB2, DB2/UDB, DB2 Tools, SQL , DB2 programming with languages like COBOL, PL1.

Moderators: Kalicharan, Moderator Group

Post Reply
deeptik
Member
Posts: 3
Joined: Fri Jul 10, 2009 1:30 pm

Updating Db2 tables in succession

Post by deeptik » Mon Jul 13, 2009 11:47 am

Hi,

There is a table T1. For each row of T1, I need to update tables T2, T3 and T4 in succession i.e if T2 update is successful, then update T3 and if T3 successful then T4 and so on. What is the shortest way to implement this logic in cobol db2 program.

thanks.

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Mon Jul 13, 2009 3:24 pm

Hi Deepti,

Welcome to mainframegurukul.com.

According to your requirement, you need to declare cursor and fetch record by
record from T1, and update T2,T3,T4 in sequence... i dont see any other shortest
way for this.

Let me know, if i did not understand your requirement correctly.
Natarajan
Chennai

deeptik
Member
Posts: 3
Joined: Fri Jul 10, 2009 1:30 pm

Post by deeptik » Mon Jul 13, 2009 3:28 pm

Hi Natarajan,

What I want to know is can we do this entire set of processing i.e updating t2, t3 , t4 in sequence for each fetched row of T1 using nested cursors instead of using separate SQL update queries.

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Mon Jul 13, 2009 3:50 pm

It is DB2 version dependent... what version of db2 you have in your shop.
Natarajan
Chennai

deeptik
Member
Posts: 3
Joined: Fri Jul 10, 2009 1:30 pm

Post by deeptik » Mon Jul 13, 2009 3:52 pm

it is V8

User avatar
Natarajan
Moderator
Posts: 537
Joined: Fri Oct 10, 2008 12:57 pm
Location: chennai
Contact:

Post by Natarajan » Mon Jul 13, 2009 5:01 pm

In my view, you can avoid Declaring curosr on T1. You cannot avoid multiple update statements.


UPDATE T2
SET XXXX = YYYY
WHERE ID = ( SELET ID FROM T1 )


Other ways...

You may update all three tables togther by declaring updatable view on these
tree tables (T2,T3,T4 ).

OR

write a Stored procedure to do update task and call that SP.
Natarajan
Chennai

Anand_Hakke
Member
Posts: 7
Joined: Wed Aug 12, 2009 12:05 pm
Location: Mumbai

Post by Anand_Hakke » Fri Aug 14, 2009 11:21 am

Please try below mthod. This is simple and good in performance point of view.

Create two triggers
trigger1- which will update table t3 after update on table t2.
trigger2 -which will update table t4 after update on table t3.

write update statement for table t2 by selecting from table t1.

the example of triggers and update statement are as below,
create trigger t2
after update of address on table2
referencing OLD as old NEW as new
FOR EACH ROW
mode db2sql
BEGIN ATOMIC
UPDATE table3 SET table3.address = new.address where table3.no = OLD.no;
END@


create trigger t3
after update of address on table3
referencing OLD as old NEW as new
FOR EACH ROW
mode db2sql
BEGIN ATOMIC
UPDATE table4 SET table4.address = new.address where table4.no = OLD.no;
END@


update table2 a set a.address =(select b.address from table1 b where b.no = a.no )@



Regards,
Anand.

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others