Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
Good morning. Is there a way of checking whether or not my cursor is open in an RPG program? I'm thinking of something like you would use for a file. Like this:
if %open(cursorname);
exec sql close cursorname;
endif;
Any help would be greatly appreciated.
That's a good question and I don't know the answer. I keep track myself with a boolean flag.
If you compiled with CLOSQLCSR *ENDMOD (which I don't recommend for performance reasons, I typically use *ENDACTGRP so ODP's can be reused by the query engine) when they will close automatically when the program exists (regardless of the *INLR value).
Chris Ringer
Hi Chris. I added the CLOSQLCSR and that seems to have taken care of the problem. BTW, this is an RPG stored procedure that is called by a job that goes into DLYW, then calls it again and times out after a certain amount of time with no activity.
Also, I wanted to thank you again for turning me on to RPG sprocs. They are awesome! I use them almost exclusively now where the sproc has to retrieve data from more than 4 files. It's the only way to go. Thanks again!
Chris
I always keep track, too. But you might be able to get it from SQLSTATE. You try to open, and if you don't get '00000', open failed. '24502', for example, means cursor identified in an OPEN statement is already open. I don't know what OPENing an already open cursor does the file pointer (first open positions at the start of the result set, I think). Of course, if it's scroll cursor and you're going to reposition anyway, that may not matter.
Just FYI, FWIW, I normally use CLOSQLCSR(*ENDACTGRP) so the SQL query optimizer can reuse ODPs (open data paths - temporary indexes) so subsequent calls to the program (module) will be fast. Typically you don't want the temporary indexes being rebuilt over and over on every call.
Here are some general rules I've learned/googled over time.
Chris Ringer