Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Figure 1: The prompt form to get parameters for the stored procedure.
Figure 2 shows the resulting HTML tables that are automatically built by Net.Data.
The tables are based on two different result sets that are returned to the macro from
the stored procedure:
• A result set containing the customer data. This result set has only one row
(record) in it.
• A result set containing the list of orders for the customer. This result set can
contain from 0…n rows.
Admittedly, the automatic HTML tables do not look very good, but the macro is
intended as a “proof of concept”. Once the result sets are returned to Net.Data, you
can use additional Net.Data table formatting functions to create better looking HTML
tables.
Figure 2: The results of running the multiple result set stored procedure.
%{***********************************************************%}
%{ Net.Data macro ResultSets.ndm %}
%{ %}
%{ Demonstrate calling stored procedure that returns %}
%{ multiple result sets. %}
%{ %}
%{ Copyright (c) 2001, Craig Pelkie %}
%{ ALL RIGHTS RESERVED %}
%{***********************************************************%}
%{***********************************************************%}
%{ Define Net.Data table variables %}
%{***********************************************************%}
%define{
DTW_DEFAULT_REPORT = "NO"
DTW_HTML_TABLE = "YES"
TABLE1 = %table
TABLE2 = %table
%}
%{***********************************************************%}
%{ SQL function to invoke multiple result set stored proc. %}
%{***********************************************************%}
%function(dtw_sql) myProc(in char(4) customerID,
in dec(3,0) districtID,
in char(4) warehouseID,
out t1,
out t2) {
call resultsets
%}
%{***********************************************************%}
%{ Macro function - uses default report processing for table %}
%{***********************************************************%}
%macro_function showTable(INOUT table) {
%}
%{***********************************************************%}
%{ HTML section - invoke stored procedure, display tables %}
%{***********************************************************%}
%html(GETTABLES) {
<html>
<head>
<title>Net.Data macro using SQL Stored Procedure</title>
</head>
<body>
<h1>Net.Data macro using SQL Stored Procedure</h1>
@myProc(FORM_CUSTOMER_ID,
FORM_DISTRICT_ID,
FORM_WAREHOUSE_ID,
TABLE1,
TABLE2)
@dtw_assign(DTW_DEFAULT_REPORT, "YES")
@showTable(TABLE1)
<hr>
@showTable(TABLE2)
</body>
</html>
%}
%{***********************************************************%}
%{ HTML section - display page, get library name %}
%{***********************************************************%}
%html(Input) {
<html>
<head>
<title>Net.Data macro using Multiple Result Set Stored
Procedure</title>
</head>
<body>
<form action="GETTABLES"
method="post">
<br>
<font color="blue" size="-1">
This macro uses multiple result sets and the parameter passing
technique.
</font>
<br>
<hr>
<input type="text"
name="FORM_CUSTOMER_ID"
size="4"
maxlength="4">
<br>
<br>
Enter the District ID (example: 001):
<input type="text"
name="FORM_DISTRICT_ID"
size="3"
maxlength="3">
<br>
<br>
<input type="text"
name="FORM_WAREHOUSE_ID"
size="4"
maxlength="4">
<br>
<br>
<input type="Submit"
value="Run Stored Procedure">
</form>
</body>
</html>
%}
****************************************************************
* Program RESULTSETS
*
* Stored procedure, return multiple result sets
*
* For use with Net.Data macro to demonstrate multiple
* result set capability of Net.Data.
*
* Use the following command to compile this program:
*
* CRTSQLRPGI OBJ(lib/RESULTSETS) SRCFILE(lib/QRPGLESRC)
*
* In the SQL environment, use the following command to
* catalog the stored procedure:
*
* create procedure resultsets
* (input p1 char(4),
* input p2 decimal(3,0),
* input p3 char(4))
* external name lib_name/resultsets
* language rpgle general
****************************************************************
C****************************************************************
C* Parameters passed to stored procedure:
C*
C* pmcust - customer ID
C* pmdistrict - district ID
C* pmwarehs - warehouse ID
C****************************************************************
C*
C *entry plist
C parm pmcust 4
C parm pmdistrict 3 0
C parm pmwarehs 4
C****************************************************************
C* Execute SELECT statement for customer file (cursor C1)
C****************************************************************
C/exec sql
C+
C+ declare C1 cursor for
C+
C+ select * from csdb/cstmr
C+
C+ where CID = :pmcust and
C+ CDID = :pmdistrict and
C+ CWID = :pmwarehs
C+
C+ for fetch only
C+
C/end-exec
C/exec sql
C+
C+ open C1
C+
C/end-exec
C****************************************************************
C* Execute SELECT statement for item file (cursor C2)
C****************************************************************
C/exec sql
C+
C+ declare C2 cursor for
C+
C+ select * from csdb/orders
C+
C+ where OCID = :pmcust and
C+ ODID = :pmdistrict and
C+ OWID = :pmwarehs
C+
C+ order by oid
C+
C+ for fetch only
C+
C/end-exec
C/exec sql
C+
C+ open C2
C+
C/end-exec
C****************************************************************
C* Set result sets to return cursor C1 and C2
C****************************************************************
C*
C/exec sql
C+
C+ set result sets cursor C1, cursor C2
C+
C/end-exec
A step at a time
It is important to understand that using stored procedures in your Net.Data macros
is not an all-or-nothing proposition. You can start by creating a stored procedure that
runs the SELECT statement that you already have in one of your macros, then
change that macro to use the stored procedure. As you develop more experience
with the technique, you may find that you prefer developing an application from
many smaller pieces that can be reused elsewhere.