MIDRANGE dot COM Mailing List Archive



Home » MIDRANGE-L » April 2013

Re: SQL Stored Procedure with Result Set



fixed

Glad it was helpful, Kurt - this is very neat stuff, I think!

----- Original Message -----
This was a very helpful message. Thanks, Vern!

I should have known about the WITH RETURN TO CLIENT bit. That's how we have our RPG Programs returning result sets.

-Kurt

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Vernon Hamberg
Sent: Wednesday, March 27, 2013 10:34 PM
To: Midrange Systems Technical Discussion
Subject: Re: SQL Stored Procedure with Result Set

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





Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2014 by MIDRANGE dot 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 here. If you have questions about this, please contact