473,382 Members | 1,766 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.

Passing a resultset from a nested Stored Procedure

Question: How do I pass a returning resultset from a nested Stored
Procedure (which opens a cursor including option "WITH RETURN TO
CALLER") as a returning resultset from it's own?

When I execute the top-level Stored Procedure, it executes succesfully,
but without any resultset.
================================================== ========
CREATE PROCEDURE sp_executesql(sqltxt CLOB(2M))
-- Executes given SQL-statement and returns resultset to caller

LANGUAGE SQL
MODIFIES SQL DATA
DYNAMIC RESULT SETS 1

BEGIN
DECLARE prefix VARCHAR(6);
DECLARE stmt CLOB(2M);

PREPARE stmt FROM sqltxt;
SET prefix = UPPER(VARCHAR(SUBSTR(sqltxt, 1, 6)));
IF prefix = 'SELECT' OR
prefix = 'VALUES' OR
SUBSTR(prefix, 1, 4) = 'WITH'
THEN
BEGIN
DECLARE res CURSOR WITH RETURN TO CALLER
FOR stmt;
OPEN res;
END;
ELSE
EXECUTE stmt;
END IF;
END
================================================== ========

When used straight from the SQL commandline, I get the result I asked
for, like
CALL sp_executesql('select * from ARTICLES') returns i.e. 100 article-records

Now I'd like to call this StoredProc from another StoredProc, like:

================================================== ========
CREATE PROCEDURE GetArticles()
LANGUAGE SQL
DYNAMIC RESULT SETS 1

BEGIN
DECLARE strSQL VARCHAR(255);
SET strSQL = 'select * from ARTICLES';
CALL sp_executesql(strSQL);
END
================================================== ========
CALL GetArticles()

it succesfully executes, but WITHOUT any result-set.
executing it once again, I get an error "Prepared statement STMT in
use." from which I understand the opened cursor remains still open.

Is there a way to get this expmaple working?

How can I store the resultset in a user-temporary table like on SQL
Server:

==============================
DECLARE @sql VARCHAR(255)
SET @sql = 'select * from ARTICLES'

INSERT INTO #temptable EXECUTE sp_executesql @sql

or

SELECT * FROM ARTICLES
INTO #temptable

==============================

Many thanks in advance.

Twan Kennis
SKB Vragenlijst Services
Amsterdam, The Netherlands

Nov 12 '05 #1
2 10557
There are 2 options for result sets:

1) return to caller - this will pass the result set back up 1 level of
nesting
2) return to client - this will return the result set directly to the
application that invoked it

There's no way to use/pass result sets between nested procedures, _and_
have the client access them.

Twan Kennis wrote:
Question: How do I pass a returning resultset from a nested Stored
Procedure (which opens a cursor including option "WITH RETURN TO
CALLER") as a returning resultset from it's own?

When I execute the top-level Stored Procedure, it executes succesfully,
but without any resultset.
================================================== ========
CREATE PROCEDURE sp_executesql(sqltxt CLOB(2M))
-- Executes given SQL-statement and returns resultset to caller

LANGUAGE SQL
MODIFIES SQL DATA
DYNAMIC RESULT SETS 1

BEGIN
DECLARE prefix VARCHAR(6);
DECLARE stmt CLOB(2M);

PREPARE stmt FROM sqltxt;
SET prefix = UPPER(VARCHAR(SUBSTR(sqltxt, 1, 6)));
IF prefix = 'SELECT' OR
prefix = 'VALUES' OR
SUBSTR(prefix, 1, 4) = 'WITH'
THEN
BEGIN
DECLARE res CURSOR WITH RETURN TO CALLER
FOR stmt;
OPEN res;
END;
ELSE
EXECUTE stmt;
END IF;
END
================================================== ========

When used straight from the SQL commandline, I get the result I asked
for, like

CALL sp_executesql('select * from ARTICLES')
returns i.e. 100 article-records

Now I'd like to call this StoredProc from another StoredProc, like:

================================================== ========
CREATE PROCEDURE GetArticles()
LANGUAGE SQL
DYNAMIC RESULT SETS 1

BEGIN
DECLARE strSQL VARCHAR(255);
SET strSQL = 'select * from ARTICLES';
CALL sp_executesql(strSQL);
END
================================================== ========

CALL GetArticles()


it succesfully executes, but WITHOUT any result-set.
executing it once again, I get an error "Prepared statement STMT in
use." from which I understand the opened cursor remains still open.

Is there a way to get this expmaple working?

How can I store the resultset in a user-temporary table like on SQL
Server:

==============================
DECLARE @sql VARCHAR(255)
SET @sql = 'select * from ARTICLES'

INSERT INTO #temptable EXECUTE sp_executesql @sql

or

SELECT * FROM ARTICLES
INTO #temptable

==============================

Many thanks in advance.

Twan Kennis
SKB Vragenlijst Services
Amsterdam, The Netherlands

Nov 12 '05 #2
Twan Kennis wrote:
Question: How do I pass a returning resultset from a nested Stored
Procedure (which opens a cursor including option "WITH RETURN TO
CALLER") as a returning resultset from it's own?


Look at the documentation for ASSOCIATE RESULT SET LOCATORS and
ALLOCATE CURSOR

jsoh

Nov 12 '05 #3

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

Similar topics

3
by: Hursh | last post by:
Hi, I have written some stored procedures in SQL and these procedures return some value. I want these values to be captured by the ASP code. I am able to access the tables using ADO(...
1
by: Harrie K. | last post by:
I want to make an Stored Procedure on a Oracle 7.x database that fills a resulset based on the values in an array: CREATE OR REPLACE PACKAGE PCK_SET IS TYPE rec_set IS RECORD ( type_id NUMBER...
1
by: June Moore | last post by:
Hi, I would like to write a stored procedure that returns a resultset. e.g. select student_id, student_subject, subject_marks from student_results where student_class_no = ? The input...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
2
by: John Spiegel | last post by:
Hi all, Any suggestions on the best way to pass a set of values to a stored procedure that will use them in a query? Specifically, I want to call a stored procedure from .NET that takes a set...
2
by: Savas Ates | last post by:
I have a stored procedure below.. When I run it with a well parameter in query analyser 3 of select statements return me. (i named select statements 1,2,3) But in asp page when i call this...
3
by: Koen | last post by:
Hi, first of all, the machine setup server 1: - UDB2 7.2.5; COUNTRY=1, location = US, IBM1250 codepage - Locale: US English; Regional Settings: English; Keyboard: Dutch; decimal separator: ....
4
by: _link98 | last post by:
Problem: java ResultSet cursor from SQL/PL stored-procedure is FORWARD_ONLY. Is it possible to have ResultSet cursors from SQL/PL procedures to scroll forward and backwards? Perhaps I am missing...
0
by: av~ | last post by:
Hi All , I am trying to call a db2 stored proc with cursor open from my java code but it always returns me null results though it executes fine when I call the procedure from the command line ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.