473,382 Members | 1,424 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,382 software developers and data experts.

DB2 cursors in stored procedures with parameters?

Hi all,

In Oracle you can pass parameters to cursors i.e.
cursor myCursor(name_city varchar(50), population integer) is
select * from cities
where name = name_city and
num_people = population;
Can you do the same thing in DB2?

All I can see is:

DECLARE myCursor CURSOR WITH RETURN FOR

and this syntax doesn't allow for parameters...
Thanks!

Dec 8 '06 #1
7 23522
<pa*************@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Hi all,

In Oracle you can pass parameters to cursors i.e.

cursor myCursor(name_city varchar(50), population integer) is
select * from cities
where name = name_city and
num_people = population;

Can you do the same thing in DB2?

All I can see is:

DECLARE myCursor CURSOR WITH RETURN FOR

and this syntax doesn't allow for parameters...

Thanks!
I am not sure what you are trying to do. Here is an example SP in DB2 that
returns a cursor based on input variable.

CREATE PROCEDURE DB2INST1.DEPT_EMP
(IN IN_DEPT CHAR(3))

RESULT SETS 1
MODIFIES SQL DATA
LANGUAGE SQL

BEGIN

DECLARE C1 CURSOR WITH RETURN FOR
SELECT EMPNO
FROM DB2INST1.EMPLOYEE
WHERE WORKDEPT = IN_DEPT;

OPEN C1;

END
-----------------------------------------------------

CALL DB2INST1.DEPT_EMP ('A00')

Result set 1
--------------

EMPNO
------
000010
000110
000120

3 record(s) selected.

Return Status = 0


Dec 8 '06 #2
Thanks for your quick reply.

Actually what I'm trying to do is get the cursor declaration to take
the parameter...

i.e. in your example the parameter is in the CREATE PROCEDURE
declaration

CREATE PROCEDURE DB2INST1.DEPT_EMP (IN IN_DEPT CHAR(3))

I want to put it in the DECLARE stmt like so:

DECLARE C1 CURSOR(param1, param2) WITH RETURN FOR

Then call it like so:

OPEN c1(param1, param2...);


Mark A wrote:
<pa*************@gmail.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
Hi all,

In Oracle you can pass parameters to cursors i.e.

cursor myCursor(name_city varchar(50), population integer) is
select * from cities
where name = name_city and
num_people = population;

Can you do the same thing in DB2?

All I can see is:

DECLARE myCursor CURSOR WITH RETURN FOR

and this syntax doesn't allow for parameters...

Thanks!

I am not sure what you are trying to do. Here is an example SP in DB2 that
returns a cursor based on input variable.

CREATE PROCEDURE DB2INST1.DEPT_EMP
(IN IN_DEPT CHAR(3))

RESULT SETS 1
MODIFIES SQL DATA
LANGUAGE SQL

BEGIN

DECLARE C1 CURSOR WITH RETURN FOR
SELECT EMPNO
FROM DB2INST1.EMPLOYEE
WHERE WORKDEPT = IN_DEPT;

OPEN C1;

END
-----------------------------------------------------

CALL DB2INST1.DEPT_EMP ('A00')

Result set 1
--------------

EMPNO
------
000010
000110
000120

3 record(s) selected.

Return Status = 0
Dec 8 '06 #3
<pa*************@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Thanks for your quick reply.

Actually what I'm trying to do is get the cursor declaration to take
the parameter...

i.e. in your example the parameter is in the CREATE PROCEDURE
declaration

CREATE PROCEDURE DB2INST1.DEPT_EMP (IN IN_DEPT CHAR(3))

I want to put it in the DECLARE stmt like so:

DECLARE C1 CURSOR(param1, param2) WITH RETURN FOR

Then call it like so:

OPEN c1(param1, param2...);
Why?

You can use a variable in the SQL statement of the cursor, and in situations
where you cannot use a variable for the SQL, then you can use a dynamically
prepared cursor with the entire definition as a parm or variable.
Dec 8 '06 #4
What you described is actually what I'm doing now (had to find a
workaround quickly =)). However it just seems that the Oracle
implementation is cleaner, and was wondering if there was an equivalent
to doing something similar.
Mark A wrote:
<pa*************@gmail.comwrote in message
news:11**********************@16g2000cwy.googlegro ups.com...
Thanks for your quick reply.

Actually what I'm trying to do is get the cursor declaration to take
the parameter...

i.e. in your example the parameter is in the CREATE PROCEDURE
declaration

CREATE PROCEDURE DB2INST1.DEPT_EMP (IN IN_DEPT CHAR(3))

I want to put it in the DECLARE stmt like so:

DECLARE C1 CURSOR(param1, param2) WITH RETURN FOR

Then call it like so:

OPEN c1(param1, param2...);

Why?

You can use a variable in the SQL statement of the cursor, and in situations
where you cannot use a variable for the SQL, then you can use a dynamically
prepared cursor with the entire definition as a parm or variable.
Dec 8 '06 #5
pa*************@gmail.com wrote:
What you described is actually what I'm doing now (had to find a
workaround quickly =)). However it just seems that the Oracle
implementation is cleaner, and was wondering if there was an equivalent
to doing something similar.
Correct - and I agree that parameterizing cursors is a nice-to-have.
It does, as Mark shows not add any function however.
You just use the variable directly within the SELECT.
If you don't know yet which variable to use when you DECLARE you can use
a temporary variable in the SELECT and assign to it before the OPEN.
All the Oracle syntax does is to father up the parameters in one place
(between the braces) and move the assignment into the OPEN syntax.

The closest I can think of that would give you at least the
encapsulation would be a table function (aka a parameterized view). But
these need to be statically created.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 8 '06 #6
Thanks again. As a developer it's always nice to have these functions.
They make life just a little easier day by day. But for now the
workaround is just as good.
Serge Rielau wrote:
pa*************@gmail.com wrote:
What you described is actually what I'm doing now (had to find a
workaround quickly =)). However it just seems that the Oracle
implementation is cleaner, and was wondering if there was an equivalent
to doing something similar.
Correct - and I agree that parameterizing cursors is a nice-to-have.
It does, as Mark shows not add any function however.
You just use the variable directly within the SELECT.
If you don't know yet which variable to use when you DECLARE you can use
a temporary variable in the SELECT and assign to it before the OPEN.
All the Oracle syntax does is to father up the parameters in one place
(between the braces) and move the assignment into the OPEN syntax.

The closest I can think of that would give you at least the
encapsulation would be a table function (aka a parameterized view). But
these need to be statically created.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 11 '06 #7
Patrick wrote:
Thanks again. As a developer it's always nice to have these functions.
They make life just a little easier day by day. But for now the
workaround is just as good.
I agree. Rest assured that I recognize a good thing when I see it. Even
if it's coming from the competition :-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 11 '06 #8

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

Similar topics

1
by: zlatko | last post by:
I use a stored procedure that is calling several other stored procedure which update or append values in several tables. All of them are stored procedures with input parameters by which they filter...
8
by: Adam Greifer | last post by:
Hi! I'm a newbie at DB2 but have 13 years of SQL Server. I need to convert over 100 SQL Server procs to DB2. I haven't had much luck with the IBM Integration Toolkit and want to avoid ER/win...
5
by: Jeff | last post by:
I have question about differences in fenced sql procedures and fenced stored procedures. Do fenced sql procedures take up an extra memory segment when executed? Reason I ask is we have several...
3
by: Zlatko | last post by:
A question concerning Access Project with SQL Server: I use a stored procedure that is calling several other stored procedure which update or append values in several tables. All of them are...
5
by: Rob Wire | last post by:
For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so...
3
by: .Net Sports | last post by:
I'm able to find info for using input parameters for stored procedures to do inserts and update type SQL functions in asp.net (vb.net), but I need to find something that will use input parameters...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
1
by: jobs | last post by:
I have a growning list of stored procedures that accept a single string as a parameter. The list and parameters are maintained in a table with two columns. Some of the Stored procedures take...
9
by: jyothi1105 | last post by:
Hi all, Here is some information which could help people who want to create stored procedures and execute them in their program. You can create stored procedures in two ways: Through front end...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.