IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Need a cursor to achieve the below requiremnt


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
sandeep kumar302

New User


Joined: 14 Mar 2012
Posts: 81
Location: India

PostPosted: Tue Mar 31, 2015 10:52 am
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

Superior Member


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

PostPosted: Tue Mar 31, 2015 3:41 pm
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: 446
Location: USA

PostPosted: Tue Mar 31, 2015 6:49 pm
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: 81
Location: India

PostPosted: Tue Mar 31, 2015 7:09 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Mar 31, 2015 7:20 pm
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: 81
Location: India

PostPosted: Tue Mar 31, 2015 7:44 pm
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: 446
Location: USA

PostPosted: Tue Mar 31, 2015 8:01 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Mar 31, 2015 8:11 pm
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

Global Moderator


Joined: 21 Sep 2010
Posts: 3053
Location: NYC,USA

PostPosted: Tue Mar 31, 2015 8:48 pm
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: 275
Location: Mumbai

PostPosted: Wed Apr 01, 2015 9:33 am
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: 1281
Location: Belgium

PostPosted: Wed Apr 01, 2015 2:27 pm
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Is SQLCODE -811 possible while fetchi... DB2 1
No new posts Restart logic by using cursor name in... DB2 1
No new posts Seeking Resolution for SQKCODE -991 o... DB2 2
No new posts Multiple rows within Cursor when Coun... DB2 14
No new posts Dynamic cursor name in Cobol program COBOL Programming 1
Search our Forums:

Back to Top