473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

set variables to be used in creation of result set.

I was wondering if there is a way to create a stored procedure that I
can
DECLARE Variables
then set these variables using a select statment -- up to here works.
then use a different statement to generate a result set.

I've been trying to do this with using a Cursor but I get an error that
I can't DECLARE a cursor after a SQL statement has been run.

I don't need it to be returned in a Cursor if there is another way. I
just need a result set.

Apr 18 '06 #1
10 1759
el*********@gmail.com wrote:
I was wondering if there is a way to create a stored procedure that I
can
DECLARE Variables
then set these variables using a select statment -- up to here works.
then use a different statement to generate a result set.

I've been trying to do this with using a Cursor but I get an error that
I can't DECLARE a cursor after a SQL statement has been run.
Wrap the DECLARE CURSOR statement in its own BEGIN ... END block.
I don't need it to be returned in a Cursor if there is another way. I
just need a result set.


Result sets and cursors are tied to each other.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 18 '06 #2
thank you.

Apr 18 '06 #3
Knut Stolze <st****@de.ibm.com> wrote:
el*********@gmail.com wrote:
I was wondering if there is a way to create a stored procedure that I
can DECLARE Variables then set these variables using a select statment
-- up to here works.
then use a different statement to generate a result set.

I've been trying to do this with using a Cursor but I get an error that
I can't DECLARE a cursor after a SQL statement has been run.


Wrap the DECLARE CURSOR statement in its own BEGIN ... END block.


Was this added in v8?

I got "...LINE 20 COL 1 NESTED COMPOUND STATEMENTS NOT ALLOWED"
as a build error when I tried that, but I'm using v7.

--
J. Moreno
Apr 25 '06 #4
J. Moreno wrote:
Knut Stolze <st****@de.ibm.com> wrote:
el*********@gmail.com wrote:
I was wondering if there is a way to create a stored procedure that I
can DECLARE Variables then set these variables using a select statment
-- up to here works.
then use a different statement to generate a result set.

I've been trying to do this with using a Cursor but I get an error that
I can't DECLARE a cursor after a SQL statement has been run.

Wrap the DECLARE CURSOR statement in its own BEGIN ... END block.


Was this added in v8?

I got "...LINE 20 COL 1 NESTED COMPOUND STATEMENTS NOT ALLOWED"
as a build error when I tried that, but I'm using v7.

DB2 for zOS presumably V7?
Try a dynamic cursor declaration, that should work.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 26 '06 #5
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote:
Knut Stolze <st****@de.ibm.com> wrote: -snip-
Wrap the DECLARE CURSOR statement in its own BEGIN ... END block.
Was this added in v8?

I got "...LINE 20 COL 1 NESTED COMPOUND STATEMENTS NOT ALLOWED"
as a build error when I tried that, but I'm using v7.

DB2 for zOS presumably V7?


Yep.
Try a dynamic cursor declaration, that should work.


A stored procedure with this....
-- start here
BEGIN
DECLARE cursor1 INSENSITIVE SCROLL CURSOR WITH RETURN FOR
SELECT count(aColumn) FROM aTable;

-- Cursor left open for client application
OPEN cursor1;
END:
-- end here

compiles. Wrapping that in an extra begin/end gets the above error
message.

--
J. Moreno
Apr 26 '06 #6
J. Moreno wrote:
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote:
Knut Stolze <st****@de.ibm.com> wrote: -snip- Wrap the DECLARE CURSOR statement in its own BEGIN ... END block.
Was this added in v8?

I got "...LINE 20 COL 1 NESTED COMPOUND STATEMENTS NOT ALLOWED"
as a build error when I tried that, but I'm using v7.

DB2 for zOS presumably V7?


Yep.
Try a dynamic cursor declaration, that should work.


A stored procedure with this....
-- start here
BEGIN
DECLARE cursor1 INSENSITIVE SCROLL CURSOR WITH RETURN FOR
SELECT count(aColumn) FROM aTable;

-- Cursor left open for client application
OPEN cursor1;
END:
-- end here

compiles. Wrapping that in an extra begin/end gets the above error
message.

Well, yes, that's what you said. Here is what I meant:

CREATE PROCEDURE p()
BEGIN
DECLARE var CHAR(1);
DECLARE txt VARCHAR(100);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET txt = 'SELECT * FROM SYSIBM.SYSDUMMY1 WHERE IBMREQD = ?';
PREPARE stmt FROM txt;
SET var = 'Y';
OPEN cur USING var;
END
@

CALL P()
@
Result set 1
--------------

IBMREQD
-------
Y

1 record(s) selected.

Return Status = 0

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 27 '06 #7
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote: -snip nested compound statement not building inside stored procedure-
compiles. Wrapping that in an extra begin/end gets the above error
message.

Well, yes, that's what you said. Here is what I meant:


Thanks for the example, I think I understand now.
CREATE PROCEDURE p()
BEGIN
DECLARE var CHAR(1);
DECLARE txt VARCHAR(100);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET txt = 'SELECT * FROM SYSIBM.SYSDUMMY1 WHERE IBMREQD = ?';
PREPARE stmt FROM txt;
SET var = 'Y';
OPEN cur USING var;
END


Doesn't build. LINE 14 COL 17 UNSUPPORTED DATA TYPE DISTINCT TYPE
ENCOUNTERED IN SQL VARIABLE STMT

Off to do some more googling...

--
J. Moreno
Apr 27 '06 #8
J. Moreno wrote:
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote:

-snip nested compound statement not building inside stored procedure-
compiles. Wrapping that in an extra begin/end gets the above error
message.

Well, yes, that's what you said. Here is what I meant:


Thanks for the example, I think I understand now.
CREATE PROCEDURE p()
BEGIN
DECLARE var CHAR(1);
DECLARE txt VARCHAR(100);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET txt = 'SELECT * FROM SYSIBM.SYSDUMMY1 WHERE IBMREQD = ?';
PREPARE stmt FROM txt;
SET var = 'Y';
OPEN cur USING var;
END


Doesn't build. LINE 14 COL 17 UNSUPPORTED DATA TYPE DISTINCT TYPE
ENCOUNTERED IN SQL VARIABLE STMT

Off to do some more googling...

Try removing
DECLARE stmt STATEMENT;

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 27 '06 #9
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote:
Serge Rielau <sr*****@ca.ibm.com> wrote: -snip-
CREATE PROCEDURE p()
BEGIN
DECLARE var CHAR(1);
DECLARE txt VARCHAR(100);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET txt = 'SELECT * FROM SYSIBM.SYSDUMMY1 WHERE IBMREQD = ?';
PREPARE stmt FROM txt;
SET var = 'Y';
OPEN cur USING var;
END


Doesn't build. LINE 14 COL 17 UNSUPPORTED DATA TYPE DISTINCT TYPE
ENCOUNTERED IN SQL VARIABLE STMT

Off to do some more googling...

Try removing
DECLARE stmt STATEMENT;


That works. Thanks, I'll get the hang of this eventually.

--
J. Moreno
Apr 27 '06 #10
J. Moreno wrote:
Serge Rielau <sr*****@ca.ibm.com> wrote:
J. Moreno wrote:
Serge Rielau <sr*****@ca.ibm.com> wrote: -snip- CREATE PROCEDURE p()
BEGIN
DECLARE var CHAR(1);
DECLARE txt VARCHAR(100);
DECLARE stmt STATEMENT;
DECLARE cur CURSOR WITH RETURN FOR stmt;
SET txt = 'SELECT * FROM SYSIBM.SYSDUMMY1 WHERE IBMREQD = ?';
PREPARE stmt FROM txt;
SET var = 'Y';
OPEN cur USING var;
END
Doesn't build. LINE 14 COL 17 UNSUPPORTED DATA TYPE DISTINCT TYPE
ENCOUNTERED IN SQL VARIABLE STMT

Off to do some more googling...

Try removing
DECLARE stmt STATEMENT;


That works. Thanks, I'll get the hang of this eventually.

Me too. I just learned why for DB2 for LUW that DECLARE is optional.
What we do for compatibility with the elder brother... :-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 28 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
2
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
37
by: Joshua Ruppert | last post by:
When I'm using FastCGI do I need to code differently than I do when using regular CGI? Because the PHP.exe processes don't go away do global variables stick around from page request to page...
4
by: Jim Carlock | last post by:
I've been using global to expose variables. However, I noticed the following... (1) global seems to use the nearest declaration. For example, // module1.php $sMyString = "Hello World.";
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
19
by: Mitesh | last post by:
Hi all, I have the following code: $req = "SELECT * FROM table1"; $res = mysql_query($req); if(!$res) return;
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.