MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Update by cursor

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Thu Jan 26, 2006 4:26 pm    Post subject: Update by cursor Reply with quote

I can use "WHERE CURRENT of Cursor_Name" when rows are selected form a single table. It wouldn't work if the FROM clause of the SELECT statement contains more than one table. But I need join another table to get the "Status" to determine if update should be applied. Is there a work around?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Fri Jan 27, 2006 6:04 am    Post subject: Reply with quote

danm,

Get the status using a Subquery and use where CURRENT of clause.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Mon Jan 30, 2006 8:58 am    Post subject: Reply with quote

kolusu,

This is what I want to accomplish:
Code:

"DECLARE SELCUR CURSOR FOR",
  "SELECT A.COL1, A.COL2, A.COL3, B.STATUS",
  "FROM TABLE A, TABLE B",
  "WHERE A.KEY1 = B.KEY1 AND A.KEY2 = B.KEY2 AND A.KEY3 = B.KEY3",
  "AND B.STATUS IN ('X','Y')",
  "FOR UPDATE OF COL1, COL2, COL3"
"OPEN SELCUR"
Do until SqlCode = 100
  "FETCH SELCUR INTO :DATA1, :DATA2, :DATA3, :STATUS"
  "UPDATE TABLEA SET COL1 = :NEWDATA1, COL2 = :NEWDATA2, COL3 = :NEWDATA3",
  "WHERE CURRENT OF SELCUR"
  /* Insert rows into table c only if stauts = 'Y' */
  If Stauts = 'Y' then "INSERT INTO TABLEC VALUES (:DATA1, :NEWDATA1,.....)
End


How do I replace the join tables by a subquery?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Jan 30, 2006 10:45 am    Post subject: Reply with quote

danm,

huh, I am confused. you are selecting col1, col2, col3 from table A and updating them with the same values?

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Mon Jan 30, 2006 11:42 am    Post subject: Reply with quote

kolusu,

No, I fetch Col1, Col2 and Col3 to Data1, Data2 and Data3, then update col1, col2 and col3 with NewData1, NewData2 and NewData3.

The followoing subquery works with "FOR UPDATE" clause:

Code:

"DECLARE SELCUR CURSOR FOR",
  "SELECT COL1, COL2, COL3",
  "FROM TABLEA A",
  "WHERE (KYE1, KEY2, KEY3)",
  "   IN ((SELECT KEY1, KEY2, KEY3", 
  "        FROM TABLEB",
  "        WHERE STATUS = 'X'",
  "        AND A.KEY1 = KEY1 AND A.KEY2 = KEY2", 
  "        AND A.KEY3 = KEY3))",
  "FOR UPDATE OF COL1, COL2, COL3"     


But I need to run the query twice, first time with STATUS = 'X' and the second time with STATUS = 'Y'. Looking for a way to run the query only once.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group