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

DB2 Stored Procedures and Dynamic SQL returning a Result Set

I'm a newbie to DB2 and am trying to figure out how to write a stored
procedure, using dynamic SQL statements to return a result set. I
believe the majority of the hurdles I have been facing might be due to
the fact that the samples and postings I have read have been related
to the UDB... I believe our company is on some version of MVS or
OS/390, but I am not sure which one, at the time of this posting.

I have an example, below, of a HelloWorld procedure that is currently
working in our environment, and would like to know how to convert it
to use a dynamic SQL statement. I have made a first attempt at
getting this to work and am also providing an example of that below.

Rather than dealing with the environmental issues I may be facing,
what I'm really hoping for is validation of the approach and syntax I
am using or guidance in getting a "Simple" HelloWorld with dynamic SQL
returning a result to work.

Be gentle on me... ;-) I am a Windows developer with Oracle and SQL
Server database experience. This MVS, OS/390 and DB2 environment is a
whole new world for me!

Thanks in advance for any assistance!
Todd

========== FUNCTIONAL HELLOWORLD PROCEDURE =========
CREATE PROCEDURE OTDSTPR.SP0200 ( IN in_client_id char(12),

OUT SQLSTATE_OUT char(5),
OUT SQLCODE_OUT int )
RESULT SETS 1
LANGUAGE SQL
MODIFIES SQL DATA
COLLID OTDSTPR
WLM ENVIRONMENT DBSOTD00
ASUTIME LIMIT 50000
RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&172.16.7.109:*)'

P1: BEGIN
-- Declare variables
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;

-- Declare Cursor
DECLARE myCursor CURSOR WITH RETURN FOR
SELECT DISTINCT
'HELLO WORLD', C.CLIENT_LONG_NM AS CLIENT
FROM CLIENT C
WHERE
(
C.CLIENT_ID = in_client_id
OR
in_client_id = 'ALL'
)
WITH UR;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
FROM SYSIBM.SYSDUMMY1;

open myCursor;

SET SQLSTATE_OUT = SQLSTATE;
SET SQLCODE_OUT = SQLCODE;

END P1
======= END FUNCTIONAL HELLOWORLD PROCEDURE =======
========== MY DYNAMIC ATTEMPT... =========
CREATE PROCEDURE OTDSTPR.SP0207 ( IN in_client_id char(12),
OUT SQLSTATE_OUT char(5),
OUT SQLCODE_OUT int )
RESULT SETS 1
LANGUAGE SQL
MODIFIES SQL DATA
COLLID OTDSTPR
WLM ENVIRONMENT DBSOTD00
ASUTIME LIMIT 50000
RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&172.16.7.109:*)'

P1: BEGIN
-- Declare variables
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;

DECLARE stmt VARCHAR(1000);
DECLARE c_clients CURSOR WITH RETURN TO CLIENT FOR s1;

SET stmt = 'SELECT CLIENT_ID, CLIENT_LONG_NM FROM OTIOTD.AF7 WHERE
CLIENT_ID = ?';
PREPARE s1 FROM stmt;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
FROM SYSIBM.SYSDUMMY1;

OPEN c_clients USING in_client_id;

SET SQLSTATE_OUT = SQLSTATE;
SET SQLCODE_OUT = SQLCODE;
END P1
========== END MY DYNAMIC ATTEMPT... =========
Nov 12 '05 #1
1 34921

"Todd Peterson" <to***********@dstoutput.com> wrote in message
news:c1**************************@posting.google.c om...
I'm a newbie to DB2 and am trying to figure out how to write a stored
procedure, using dynamic SQL statements to return a result set. I
believe the majority of the hurdles I have been facing might be due to
the fact that the samples and postings I have read have been related
to the UDB... I believe our company is on some version of MVS or
OS/390, but I am not sure which one, at the time of this posting.

I have an example, below, of a HelloWorld procedure that is currently
working in our environment, and would like to know how to convert it
to use a dynamic SQL statement. I have made a first attempt at
getting this to work and am also providing an example of that below.

Rather than dealing with the environmental issues I may be facing,
what I'm really hoping for is validation of the approach and syntax I
am using or guidance in getting a "Simple" HelloWorld with dynamic SQL
returning a result to work.

Be gentle on me... ;-) I am a Windows developer with Oracle and SQL
Server database experience. This MVS, OS/390 and DB2 environment is a
whole new world for me!

Thanks in advance for any assistance!
Todd

========== FUNCTIONAL HELLOWORLD PROCEDURE =========
CREATE PROCEDURE OTDSTPR.SP0200 ( IN in_client_id char(12),

OUT SQLSTATE_OUT char(5),
OUT SQLCODE_OUT int )
RESULT SETS 1
LANGUAGE SQL
MODIFIES SQL DATA
COLLID OTDSTPR
WLM ENVIRONMENT DBSOTD00
ASUTIME LIMIT 50000
RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&172.16.7.109:*)'

P1: BEGIN
-- Declare variables
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;

-- Declare Cursor
DECLARE myCursor CURSOR WITH RETURN FOR
SELECT DISTINCT
'HELLO WORLD', C.CLIENT_LONG_NM AS CLIENT
FROM CLIENT C
WHERE
(
C.CLIENT_ID = in_client_id
OR
in_client_id = 'ALL'
)
WITH UR;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
FROM SYSIBM.SYSDUMMY1;

open myCursor;

SET SQLSTATE_OUT = SQLSTATE;
SET SQLCODE_OUT = SQLCODE;

END P1
======= END FUNCTIONAL HELLOWORLD PROCEDURE =======
========== MY DYNAMIC ATTEMPT... =========
CREATE PROCEDURE OTDSTPR.SP0207 ( IN in_client_id char(12),
OUT SQLSTATE_OUT char(5),
OUT SQLCODE_OUT int )
RESULT SETS 1
LANGUAGE SQL
MODIFIES SQL DATA
COLLID OTDSTPR
WLM ENVIRONMENT DBSOTD00
ASUTIME LIMIT 50000
RUN OPTIONS 'NOTEST(ALL,*,,VADTCPIP&172.16.7.109:*)'

P1: BEGIN
-- Declare variables
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLCODE INT DEFAULT 0;

DECLARE stmt VARCHAR(1000);
DECLARE c_clients CURSOR WITH RETURN TO CLIENT FOR s1;

SET stmt = 'SELECT CLIENT_ID, CLIENT_LONG_NM FROM OTIOTD.AF7 WHERE
CLIENT_ID = ?';
PREPARE s1 FROM stmt;

DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT SQLSTATE, SQLCODE INTO SQLSTATE_OUT, SQLCODE_OUT
FROM SYSIBM.SYSDUMMY1;

OPEN c_clients USING in_client_id;

SET SQLSTATE_OUT = SQLSTATE;
SET SQLCODE_OUT = SQLCODE;
END P1
========== END MY DYNAMIC ATTEMPT... =========


It is very difficult to teach programming techniques to someone via a
newgroup.

I think your best bet is to determine exactly what version of DB2 you are
using and which OS you are on, then read the Application Development Guide
for that version and platform. The Application Development Guide, in
conjunction with the SQL Reference, should tell you enough to get you going
in the right direction. Also, consult the examples that came with your copy
of DB2. You should find examples of what you are trying to do in the Samples
directory, most of which are explained in the same two manuals, Application
Development Guide and SQL Reference.

The manuals for DB2 on z/OS (the new name for MVS and OS/390) and DB2 on
Linux, Unix and Windows, can be found at:
http://www-306.ibm.com/software/data...nfocenters.jsp.
You should be able to find the manuals there. The manuals should indicate
exactly where the sample programs are too.

The other alternative I can suggest is that you take DB2 courses from either
IBM or other vendors. You should be able to find courses in your area that
cover development of stored procedures for your platform. It will cost you
(or your management) some money but you'll probably learn the concept and
techniques better than if you try to teach yourself from the manuals.

Rhino
Nov 12 '05 #2

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

Similar topics

0
by: shamansc | last post by:
I am trying to use RDB stored procedures to return result sets. How do I implement this in RDB? For example to select all employees in a table select * from employees How would I return a...
9
by: Ralph Cramden | last post by:
I'm writing a VB6 app which calls several stored procedures on my SQL Server DB. The first stored procedure must complete its inserts before the second stored procedure can query the modified...
11
by: Bã§TãRÐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
1
by: Eugene Anthony | last post by:
Method 1: set rs = Server.CreateObject("ADODB.Recordset") objConn.usp_RetrieveCategories rs Method 2: set rs = objConn.Execute("usp_RetriveCategories") Which method is considered to...
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
4
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
7
by: eholz1 | last post by:
Hello PHP group, Could someone help me out? I have PHP 5.2, Apache 2.0, and MySQL 5.0 running on Linux (Redhat Fedora Core 6). All that works fine. I would like to be able to "call" a stored...
3
by: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using...
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: 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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.