View previous topic :: View next topic
|
Author |
Message |
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
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 |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
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 |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
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 |
|
|
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
I tested both the queries but none worked.
Both are giving me result with office_type other than 'HOME OFFICE' record. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
sandeep kumar302
New User
Joined: 14 Mar 2012 Posts: 81 Location: India
|
|
|
|
Hi Enrico,
Actually we have to use cursor only to achieve this. |
|
Back to top |
|
|
RahulG31
Active User
Joined: 20 Dec 2014 Posts: 446 Location: USA
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
Quote: |
Actually we have to use cursor only to achieve this. |
|
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3053 Location: NYC,USA
|
|
|
|
Make a use of UNION or UNION ALL as per your requirement |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
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 |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
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 |
|
|
|