Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Need a cursor to achieve the below requiremnt

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
sandeep kumar302

New User


Joined: 14 Mar 2012
Posts: 69
Location: India

PostPosted: Tue Mar 31, 2015 10:52 am    Post subject: Need a cursor to achieve the below requiremnt
Reply with quote

I have a requirement IN DB2 like below which i want to achieve using a cursor :
=============================================================================

1. Read first record in table TEMP_TABLE using
COUNTRY_CD = ISO COUNTRY CD
NATIONAL_ID = bank/branch
OFFICE_TYPE = 'HOME OFFICE'
GROUP_TYPE = 'Parent'

If found, then bank/branch is valid.

2. If the above is not found. Read first TEMP_TABLE using
COUNTRY_CD = ISO COUNTRY CD
NATIONAL_ID = bank/branch
OFFICE_TYPE = 'HOME OFFICE'

If found, then bank/branch is valid

3. If the above is not found. Read first TEMP_TABLE using
COUNTRY_CD = ISO COUNTRY CD
NATIONAL_ID = bank/branch

If found, then bank/branch is valid

4. If the above is not found. Then the bank/branch is invalid

can i achieve the above 4 points using a single cursor, if yes how. Do i need to use while/where in my fetch statement?
I can achieve this using 3 different select query but i want to achive this using cursor, can some help me on that?

Actually i am using microfocus cobol and oracle is the database but the cursor concept in DB2 is similar to oracle so i
have mentioned that i will be using DB2 as the similar concept i will apply for oracle in my case.
Back to top
View user's profile Send private message

Anuj Dhawan

Senior Member


Joined: 22 Apr 2006
Posts: 6258
Location: Mumbai, India

PostPosted: Tue Mar 31, 2015 3:41 pm    Post subject:
Reply with quote

If you put COUNTRY_CD and NATIONAL_ID with an "AND" and rest two columns with "OR" - should not that suffice for your requirement? Something on the lines of (not tested):

Code:
EXEC SQL DECLARE C1 CURSOR FOR
     SELECT COUNTRY_CD,NATIONAL_ID,OFFICE_TYPE,GROUP_TYPE
     FROM TEMP_TABLE
     WHERE (COUNTRY_CD = ISO_COUNTRY_CD AND
                 NATIONAL_ID = "bank/branch") OR
                ( OFFICE_TYPE = 'HOME OFFICE' OR
                GROUP_TYPE = 'Parent')
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 331
Location: USA

PostPosted: Tue Mar 31, 2015 6:49 pm    Post subject: Reply to: Need a cursor to achieve the below requiremnt
Reply with quote

I somewhat agree with comments made by Anuj, but my query will have all ANDs with ORs on the value for last 2 fields (Not Tested):

Code:
EXEC SQL DECLARE C1 CURSOR FOR
     SELECT COUNTRY_CD,NATIONAL_ID,OFFICE_TYPE,GROUP_TYPE
     FROM TEMP_TABLE
     WHERE COUNTRY_CD = ISO_COUNTRY_CD AND
                 NATIONAL_ID = "bank/branch" AND
                ( OFFICE_TYPE = 'HOME OFFICE' OR OFFICE_TYPE = ' ') AND
                ( GROUP_TYPE = 'Parent' OR GROUP_TYPE = ' ' )
ORDER BY OFFICE_TYPE, GROUP_TYPE DESC

I am using descending in case there are multiple rows fetched then the rows with a value comes on top (rather than space or null) and you fetch the first row.
Back to top
View user's profile Send private message
sandeep kumar302

New User


Joined: 14 Mar 2012
Posts: 69
Location: India

PostPosted: Tue Mar 31, 2015 7:09 pm    Post subject:
Reply with quote

I tested both the queries but none worked.

Both are giving me result with office_type other than 'HOME OFFICE' record.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Tue Mar 31, 2015 7:20 pm    Post subject: Reply to: Need a cursor to achieve the below requiremnt
Reply with quote

the proper way to post the question IS NOT
Quote:
I have a requirement IN DB2 like below which i want to achieve using a cursor :

but just
Quote:
I have a requirement IN DB2 like below ...


Quote:
I can achieve this using 3 different select query but i want to achive this using cursor, can some help me on that?

do You have a doctor' s prescription to do it using a cursor ???
Back to top
View user's profile Send private message
sandeep kumar302

New User


Joined: 14 Mar 2012
Posts: 69
Location: India

PostPosted: Tue Mar 31, 2015 7:44 pm    Post subject:
Reply with quote

Hi Enrico,
icon_lol.gif icon_lol.gif

Actually we have to use cursor only to achieve this.
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 331
Location: USA

PostPosted: Tue Mar 31, 2015 8:01 pm    Post subject: Reply to: Need a cursor to achieve the below requiremnt
Reply with quote

The query works but it may Not work exactly the way you want it to. You also need to make a little effort to make it perfect for your work.
Code:
                ( OFFICE_TYPE = 'HOME OFFICE' OR OFFICE_TYPE = ' ') AND
                ( GROUP_TYPE = 'Parent' OR GROUP_TYPE = ' ' )

So, instead of comparison with spaces, you may need to compare with nulls (i.e. office type and group type) AND you may also need to think of a logic so that the row comes on top (Not necessarily with Order by desc clause for both the fields). There may be other fields in the table on which you can use the Order by clause to get the sequence of records you want.

*I've tested it on Access as I do not have DB2 at my place.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Tue Mar 31, 2015 8:11 pm    Post subject: Reply to: Need a cursor to achieve the below requiremnt
Reply with quote

Quote:
Actually we have to use cursor only to achieve this.

icon_eek.gif
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Tue Mar 31, 2015 8:48 pm    Post subject:
Reply with quote

Make a use of UNION or UNION ALL as per your requirement
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 269
Location: Mumbai

PostPosted: Wed Apr 01, 2015 9:33 am    Post subject:
Reply with quote

Hi,

Agree with Rohit, either use Union or try with below query ( Not tested but should work)
Code:

DECLARE C1 CURSOR FOR
     SELECT COUNTRY_CD,NATIONAL_ID,OFFICE_TYPE,GROUP_TYPE
     FROM TEMP_TABLE
     WHERE (COUNTRY_CD = ISO_COUNTRY_CD             AND
                 NATIONAL_ID = "bank/branch"        AND
                 OFFICE_TYPE = 'HOME OFFICE'        AND
                GROUP_TYPE = 'Parent' )
                             OR
              (COUNTRY_CD = ISO_COUNTRY_CD          AND
                 NATIONAL_ID = "bank/branch"        AND
                OFFICE_TYPE = 'HOME OFFICE' )
                            OR
             (COUNTRY_CD = ISO_COUNTRY_CD          AND
                 NATIONAL_ID = "bank/branch"  )


But how the table data will look like? Table will have rows satisfying all conditions at a given time?

Regards,
Chandan
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Wed Apr 01, 2015 2:27 pm    Post subject:
Reply with quote

if you only need to test for validity ,
only the last query is needed :
Code:
COUNTRY_CD = :ISO-COUNTRY-CD             AND
NATIONAL_ID = :bankbranch-id

when sqlcode 100 => invalid
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Updating Cursor row withour using FOR... chandan.inst DB2 15 Tue Nov 08, 2016 11:17 am
No new posts Variable for cursor names ravikumar15 DB2 5 Sun Jul 17, 2016 7:08 am
No new posts DB2 Stored Procedure - Dynamic SQL - ... GuyC DB2 2 Wed Feb 17, 2016 5:10 pm
No new posts ISREDIT - Position Cursor on the Comm... Tarique Anwer TSO/ISPF 5 Wed Feb 03, 2016 12:44 pm
No new posts CURSOR Clocking sprikitik DB2 1 Tue Mar 17, 2015 12:17 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us