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
 

 

Replace UNION by multiple SELECT Statments inside a cursor?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> HomeWorks & Requests
View previous topic :: :: View next topic  
Author Message
Vithiyalan

New User


Joined: 15 Nov 2011
Posts: 6
Location: India

PostPosted: Thu Jan 19, 2012 5:18 pm    Post subject: Replace UNION by multiple SELECT Statments inside a cursor?
Reply with quote

Can someone tell me how to replace this UNION statement by simple SELECT Statements inside a CURSOR declare


EXEC SQL
DECLARE CRSR1 CURSOR FOR
SELECT EMP_NBR
, EMP_CONT
, CRT_TS
, TYPE_IND
, FLG
FROM TABLEA
WHERE EMP_NBR = :WS-EMP-NBR
AND TYPE_IND IN ('CL','TI','RM','CP','FA')
AND FLG = 'Y'
AND CRT_TS > :WS-LOW-RUN-TS
AND CRT_TS < :WS-HIGH-RUN-TS

UNION

SELECT A.EMP_NBR
, A.EMP_CONT
, A.CRT_TS
, A.TYPE_IND
, A.FLG
FROM TABLEA A
,TABLEB B
WHERE EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.TYPE_IND = B.TYPE_IND
AND B.LAST_UPD_TS > :WS-LOW-RUN-TS
AND B.LAST_UPD_TS < :WS-HIGH-RUN-TS

UNION

SELECT A.EMP_NBR
, A.EMP_CONT
, A.CRT_TS
, A.TYPE_IND
, A.FLG
FROM TABLEA A
, TABLEC B
WHERE A.EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.EMP_NBR = B.EMP_NBR
AND A.EMP_CONT = B.EMP_CONT
AND B.FLG = 'N'
AND B.NEW_FLG = 'Y'
AND B.AUD_CRT_TS > :WS-LOW-RUN-TS
AND B.AUD_CRT_TS < :WS-HIGH-RUN-TS

WITH UR
END-EXEC
Back to top
View user's profile Send private message

chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 269
Location: Mumbai

PostPosted: Fri Jan 20, 2012 11:59 am    Post subject:
Reply with quote

Hi,

What you have tried so far for this?

Change your select clause to have DISTINCT and WHERE Clause as below and see if it works as its not tested

Code:
SELECT  DISTINCT
   EMP_NBR
, EMP_CONT
, CRT_TS
, TYPE_IND
, FLG
FROM TABLEA
         ,,TABLEB B
WHERE
(EMP_NBR = :WS-EMP-NBR
AND TYPE_IND IN ('CL','TI','RM','CP','FA')
AND FLG = 'Y'
AND CRT_TS > :WS-LOW-RUN-TS
AND CRT_TS < :WS-HIGH-RUN-TS )
OR
(EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.TYPE_IND = B.TYPE_IND
AND B.LAST_UPD_TS > :WS-LOW-RUN-TS
AND B.LAST_UPD_TS < :WS-HIGH-RUN-TS)   
OR
( A.EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.EMP_NBR = B.EMP_NBR
AND A.EMP_CONT = B.EMP_CONT
AND B.FLG = 'N'
AND B.NEW_FLG = 'Y'
AND B.AUD_CRT_TS > :WS-LOW-RUN-TS
AND B.AUD_CRT_TS < :WS-HIGH-RUN-TS )


You need to check performance of this query as compared to previous one.

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

New User


Joined: 15 Nov 2011
Posts: 6
Location: India

PostPosted: Fri Jan 20, 2012 3:49 pm    Post subject: Reply to: Replace UNION by multiple SELECT Statments inside
Reply with quote

Hi,
Thanks a lot for your help. It worked, however i didnt checked the performance. I have a another query to be solved. This is the query.
I have added 'SYSTEM' and 'USER' to the SELECT statments. Could you please help

EXEC SQL
DECLARE CRSR1 CURSOR FOR
SELECT EMP_NBR
, EMP_CONT
, CRT_TS
, TYPE_IND
, FLG
, 'SYSTEM'
FROM TABLEA
WHERE EMP_NBR = :WS-EMP-NBR
AND TYPE_IND IN ('CL','TI','RM','CP','FA')
AND FLG = 'Y'
AND CRT_TS > :WS-LOW-RUN-TS
AND CRT_TS < :WS-HIGH-RUN-TS

UNION

SELECT A.EMP_NBR
, A.EMP_CONT
, A.CRT_TS
, A.TYPE_IND
, A.FLG
FROM TABLEA A
,TABLEB B
,SYSTEM'
WHERE EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.TYPE_IND = B.TYPE_IND
AND B.LAST_UPD_TS > :WS-LOW-RUN-TS
AND B.LAST_UPD_TS < :WS-HIGH-RUN-TS

UNION

SELECT A.EMP_NBR
, A.EMP_CONT
, A.CRT_TS
, A.TYPE_IND
, A.FLG
, 'USER'
FROM TABLEA A
, TABLEC B
WHERE A.EMP_NBR = :WS-EMP-NBR
AND A.TYPE_IND IN ('CL','TI','RM','CP','FA')
AND A.FLG = 'Y'
AND A.EMP_NBR = B.EMP_NBR
AND A.EMP_CONT = B.EMP_CONT
AND B.FLG = 'N'
AND B.NEW_FLG = 'Y'
AND B.AUD_CRT_TS > :WS-LOW-RUN-TS
AND B.AUD_CRT_TS < :WS-HIGH-RUN-TS

WITH UR
END-EXEC
Back to top
View user's profile Send private message
Pandora-Box

Moderator


Joined: 07 Sep 2006
Posts: 1534
Location: Andromeda Galaxy

PostPosted: Fri Jan 20, 2012 4:27 pm    Post subject:
Reply with quote

Is SYSTEM & USER are fields or Table names?

I guess its they are fields please confirm.
Back to top
View user's profile Send private message
Vithiyalan

New User


Joined: 15 Nov 2011
Posts: 6
Location: India

PostPosted: Fri Jan 20, 2012 5:04 pm    Post subject:
Reply with quote

Its like a character constant that is appended to the end of the result. It is just to identify like from which query the row was picked
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 -> HomeWorks & Requests All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Data replication from multiple Db2 ta... kishpra DB2 1 Mon Mar 27, 2017 9:58 pm
No new posts Unable to catalog a gdg dataset resid... Shovan JCL & VSAM 7 Fri Mar 24, 2017 2:24 pm
No new posts Getting -504 Cursor Name GTT-ARTS-CUR... Robin Sulsona DB2 2 Fri Mar 17, 2017 1:43 am
No new posts To display values of multiple rows in... vinu78 DB2 2 Thu Mar 09, 2017 1:20 pm
No new posts Release of Adabas Cursor mohitsethi All Other Mainframe Topics 1 Mon Feb 06, 2017 8:36 pm


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