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 

SQL Cursor in PL1

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


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Wed Feb 08, 2012 3:16 pm    Post subject: SQL Cursor in PL1 Reply with quote

I want to order by DESC or ASC depending the value of a Flag:
Code:

If Flag = '1'B then
  EXEC SQL DECLARE CURTMP CURSOR FOR
    SELECT COL1
    FROM TABLE
    WHERE COL2 = :VALUE2
    ORDER BY COL1 DESC;
Else
   EXEC SQL DECLARE CURTMP CURSOR FOR
    SELECT COL1
    FROM TABLE
    WHERE COL2 = :VALUE2
    ORDER BY COL1 ASC;

EXEC SQL OPEN CURTMP;
EXEC SQL FETCH CURTMP INTO :FLD:NULLFLD;
.......
.......
EXEC SQL CLOSE CURTMP;

When complied, an error : CURSOR "CURTEMP" PREVIOUSLY DECLARED OR REFERENCED.
I can fix the problem by declaring two cursors and duplicating the codes.
Code:

If Flag = '1'B then do;
  EXEC SQL DECLARE CURTMP CURSOR FOR
    SELECT COL1
    FROM TABLE
    WHERE COL2 = :VALUE2
    ORDER BY COL1 DESC;
  EXEC SQL OPEN CURTMP;
  EXEC SQL FETCH CURTMP INTO :FLD:NULLFLD;
  .......
  .......
  EXEC SQL CLOSE CURTMP;
End;
Else do;
  EXEC SQL DECLARE CURTMP1 CURSOR FOR
    SELECT COL1
    FROM TABLE
    WHERE COL2 = :VALUE2
    ORDER BY COL1 ASC;
  EXEC SQL OPEN CURTMP1;
  EXEC SQL FETCH CURTMP1 INTO :FLD:NULLFLD;
  .......
  .......
  EXEC SQL CLOSE CURTMP1;
End;

How can I avoid duplicate codes?
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: Wed Feb 08, 2012 4:35 pm    Post subject: Reply with quote

danm,

what is the format of the column you are fetching? is it numeric?

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Thu Feb 09, 2012 9:18 am    Post subject: Reply with quote

Since the coding you want to change is part of an EXEC SQL statement, you can only perform this by using dynamic SQL. Within static SQL, as you are using it, this isn't possible.

regards
Christian
Back to top
View user's profile Send private message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Thu Feb 09, 2012 9:36 am    Post subject: Reply with quote

Kolusu,

It is a small integer.
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Feb 09, 2012 10:47 am    Post subject: Reply with quote

other than dynamic sql as CZerfas has stated
one way to do it so that you only have one imbedded cursor
would be to use as CASE clause in your select statement
CASE WHEN :FLAG = 1 THEN COL1 ELSE -1 * COL1 END
with a DESC ORDER BY clause.

but then you would also have to have PL1 code to convert the host variable for COL1 based on your flag.

using two cursors means two sets of logic processing the different cursors.
using one imbedded cursor with the CASE logic means having a compare and a multiply in your PL1 code.

if this is only an esoteric exercise, use the CASE logic.
but if this is for an actual program, use two cursors or dynamic sql.
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
danm
Intermediate


Joined: 29 Jun 2004
Posts: 170
Topics: 73

PostPosted: Thu Feb 09, 2012 11:07 am    Post subject: Reply with quote

CZerfas and dbzTHEdinosauer,

It is for an actual program. I will use with the 2 cursors approach and static sql. Thanks.
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: Thu Feb 09, 2012 12:27 pm    Post subject: Reply with quote

danm,

If it was a numeric column I was going to suggest what DBZ have suggested i.e multiply the column with -1 and always use the ASC sequence which is the default. I guess 2 static sql's are better for understanding and at any point you will be dealing with just 1 active cursor.

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: Thu Feb 09, 2012 4:00 pm    Post subject: Reply with quote

Thank you all.
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 -> Application Programming 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