× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Kurt

I've been "learning through fighting" as you have - I was looking for a place that'd tell me the order of things, because I got all these "invalid token" kinds of messages.

For a while I thought the documentation for order isn't to be found. But I finally did find it - although not explicitly stated as something like "here is the order of things for a stored procedure", which is what I wanted to see. But nothing obvious, even in the SQL Programming manual.

I did know that at some release, things were changed with embedded SQL, so that DECLARE CURSOR had to be the "first thing".

And no examples that showed everything in one place, and I am doing almost everything one can - haven't done a return-codes-declaration yet - if that's the right name!

What I eventually found was a combination. The "compound-statement" description has the order, by implication that what it presents HAS to be that way.

That's in Chapter 6, in the section on 'compound-statement'. Now I also need a global temporary table - neat thing, that! But there's nothing about that in the "compound-statement". I'd tried putting it all over the place.

Then I saw the component of the 'compound-statement' labeled as 'SQL-procedure-statement', which is statements that can be executed within a control statement, and that includes IF and WHILE - AND COMPOUND-STATEMENT!!!

So I also wanted to declare variables within the body - but it turns out, they all have to be together at the top.

So the order is -

1. Variable, condition, and return-code declarations
2. Cursor declarations
3. Handler declarations
4. SQL procedure statement(s) - and see THAT section to see what can be done there

It seems compound statements can be nested or consecutive - I think. But there is scope and visibility that can be controlled, it seems, with the use of labels.

I believe the 4 items above can be found within each compound statement - anything delimited by BEGIN and END and labeled.

OK, enough - am giving away all the secrets!! I have a feeling that non-IBM i docs on stored procedures might do better than what we have - need to look around.

Oh - one thing - I found if I declare a variable without default, and if I try to use it in a comparison, it fails - the variable is NULL! Got bit a few times these last few days!

Another thing - the WITH RETURN defaults to WITH RETURN TO CALLER - I read in some example that it is a good idea to use TO CLIENT when using JDBC to call the stored procedure - this gets the RETURN all the way out, I believe. Maybe like *PGMBDY for ILE message handling.

OK, had enough? :) This is really neat stuff, I think - I'll be doing more of it, for sure. Thanks for bringing up the issues, this is helping me out a lot.

Oh, I did find that I can both return result sets and set a return value - just put the RETURN; statement after the OPEN C1;

Vern

On 3/27/2013 9:29 AM, Anderson, Kurt wrote:
Thanks for all of the responses. I have modified my declare to have WITH RETURN in it.

With a fresh pair of eyes, I determined that SQL is finicky about where the Declares are in the code. I originally had this Declare Cursor at the end of the procedure. Moving it wasn't so easy, it seems SQL is extra finicky. The Declare Cursor had to come after the regular variable Declares, but before the Declare Exit Handler.

However - it works!

I'm still not a fan of the funky result set Select. Maybe it should be a function, though my understanding was that any kind of complexity or i/o was a sign one should use a procedure instead of a function.

-Kurt

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Michael Schutte
Sent: Wednesday, March 27, 2013 8:01 AM
To: Midrange Systems Technical Discussion
Subject: Re: SQL Stored Procedure with Result Set

When a Stored Procedure is exited, all open cursors will be returned as result sets.

You have the create procedure correct.

Result Sets 1

However change the DECLARE to...

DECLARE rsCursor CURSOR WITH RETURN FOR SELECT rtnTracking FROM
sysibm/sysdummy1 ;

The "WITH RETURN" is key.

Then just simply

OPEN rsCURSOR

Something I've had issue with when trying to sql procedures repeatedly from RPG is that they cursor will remain open. So I've added additional code that makes sure that the cursor is closed first.

Here is an example of what I do.


BEGIN
DECLARE CURSOR_NOT_OPENED CONDITION FOR SQLSTATE '24501' ; DECLARE CURSOROPEN CHAR ( 1 ) ; DECLARE C1 CURSOR WITH RETURN FOR
SELECT IAITEM , IADSC1 , IADATE , IATRAN , IASREF , IATYPE , IAQTY
FROM INVACTF
ORDER BY IAITEM , IADATE , IATYPE , IATRAN;

Open C1;
END



FWIW, It's seems to me that you are returning a single value. So why not a function (SQL UDF) instead?





On Tue, Mar 26, 2013 at 8:24 PM, Vernon Hamberg <vhamberg@xxxxxxxxxxxxxxx>wrote:

Buck, I think Kurt is doing an SQL procedure - not an external RPG
procedure. We don't use host variables in those things, and I haven't
checked about using SET RESULT SETS ARRAY in the context of an SQL
procedure.

Ever learning, I hope!

Vern

On 3/26/2013 5:25 PM, Buck Calabro wrote:
On 3/26/2013 5:48 PM, Anderson, Kurt wrote:
I have a stored procedure and it was returning an output parameter.
People are whining because of the effort they have to go through to
consume an output parameter (in Java). Ok, fine (yet here I am, going
through 'effort,' but I like to learn so I guess that's the difference).
So I'm trying to switch the stored procedure to return the SmallInt
value as a single result set. However it doesn't seem to be working.
In the create procedure statement, after the parameter list, I have:
Result Sets 1
Language SQL

At the very bottom of the procedure (just before the "End"), I have:
DECLARE rsCursor CURSOR FOR SELECT rtnTracking FROM
sysibm/sysdummy1;
OPEN rsCursor;
SET RESULT SETS CURSOR rsCursor;

rtnTracking is a local variable:
Declare rtnTracking SmallInt;

First, I'm getting an error.
Second, I hope there's a better way to set a local variable to a
result
set w/o doing a "fake" select.
The error (on the Declare Cursor line):
Position 13 Token RSCURSOR was not valid. Valid tokens: GLOBAL.

I'm at IBM i 7.1.
You don't have to make a fake SQL call in order to return a result
set; You can return an array - even an array of one element.

// this is the result set we'll be returning
// the names here are what the caller will see
drs ds dim(1000) qualified
d count 10i 0
d name 50 varying

// here's the part that tells the database manager how to handle //
the result set exec sql
set result sets array :RS for :ROWCOUNT rows;

*inlr = *on;

--buck
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing
list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe,
unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take
a moment to review the archives at
http://archive.midrange.com/midrange-l.


--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/midrange-l.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.