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

Combining stored procedure and a select statement - possible?

Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a
select statement? I mean something like this:

Select
c.number,
call procedure( c.number ) as list
from
table c

With best regards
TN

Nov 12 '05 #1
6 26486
Perhaps you are thinking of a User-Defined Function (UDF)?

--
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
"Terentius Neo" <te***********@here.fi> wrote in message
news:B2**************@read3.inet.fi...
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a select
statement? I mean something like this:

Select
c.number,
call procedure( c.number ) as list
from
table c

With best regards
TN


Nov 12 '05 #2
Terentius Neo wrote:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a
select statement? I mean something like this:

Select
c.number,
call procedure( c.number ) as list
from
table c

No. The SQL Standard has a clear distinction between procedures and
functions. What you need is a table function.

Procedures can have INOUT or OUT parameters and also return multiple (or
no) result sets. Neither can be fitted into the model (although some
vendors are mating what IMHO mustn't be mated).

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
Larry Menard wrote:
Perhaps you are thinking of a User-Defined Function (UDF)?


Yes, perhaps :-) If a UDF can loop through an other table and return one
string compiled from the information in the table. Can it be done?

TN

Nov 12 '05 #4
Terentius Neo wrote:
Larry Menard wrote:
Perhaps you are thinking of a User-Defined Function (UDF)?

Yes, perhaps :-) If a UDF can loop through an other table and return one
string compiled from the information in the table. Can it be done?

That smells more like a scalar function to me, but hey, your wish is my
command:

db2 -td@
CREATE FUNCTION CONCAT_AGG(a INT) RETURNS TABLE(str VARCHAR(1000))
BEGIN ATOMIC
DECLARE str VARCHAR(1000) DEFAULT '';
FOR row AS SELECT c1 FROM T WHERE c2 = a DO
SET str = c1 || ',' || str;
END FOR;
RETURN VALUES(SUBSTR(str, 1, LENGTH(str - 1));
END
@

A user defined aggregate function (untested), sort of...

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Serge Rielau wrote:
That smells more like a scalar function to me, but hey, your wish is my
command: <snip> A user defined aggregate function (untested), sort of...


Excellent! Thanks for the help.

Terentius
Nov 12 '05 #6
I agree with Serge. It looks like you either want a table function or a
stored procedure that returns a result set.

You'd invoke a table function like:

SELECT <columns> FROM TABLE(UDF(number)) AS TableUDF

<columns>
================
<result1>
<result2>
....
Table UDFs are hard to describe here, but essentially they are passed
the input data, and depending on the type of call (also passed in) will
either open the data for return, process the data for a fetch, or clean
up after everything is done.

Alternately, if you simply want to call a procedure and pass it a value,
and have it run a query against a table for you based on that value,
stored procedures have the ability to return result sets to the
immediate caller of the procedure (or, for C & SQL procedures, to the
client application).

For C/SQL, this is something like:

DECLARE CURSOR C1 WITH RETURN TO [CALLER | CLIENT] FOR <etc>;

Each language has different rules, and depending on the language may
even have different rules for the parameter style. For instance,
LANGUAGE C uses the same method for each parameter style supported, but
for LANGUAGE JAVA, PARAMETER STYLE DB2GENERAL and PARAMETER STYLE JAVA
handle returning resultsets quite differently. Check the documentation
for more details.

Note that you'll need to check to see how your client supports handling
result sets returned from procedures, as it varies by client. For
instance, I don't think embedded C applications can handle them
(somewhat odd considering an embedded C procedure can OPEN a resultset
for returning), but anything that's CLI (JAVA/.NET/CLP) based will do so
with no problems.

I'm not quite sure which scenario fits your needs. Typically, you'd use
a stored procedure that returns one (or more) result sets if you simply
want to centralize a few queries rather than distributing them among
multiple applications. A table function, on the other hand, is useful
if you want to convert non-DB2 data into a query, such as passing in a
name of a formatted data file as input into the table UDF, opening that
file, and fetching results from it.

I hope that helps!

Serge Rielau wrote:
Terentius Neo wrote:
Is it possible to combine (in DB2 UDB 8.1) a stored procedure and a
select statement? I mean something like this:

Select
c.number,
call procedure( c.number ) as list
from
table c


No. The SQL Standard has a clear distinction between procedures and
functions. What you need is a table function.

Procedures can have INOUT or OUT parameters and also return multiple (or
no) result sets. Neither can be fitted into the model (although some
vendors are mating what IMHO mustn't be mated).

Cheers
Serge

Nov 12 '05 #7

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

Similar topics

4
by: mjuricek | last post by:
I'm having some problems to optimize my stored procedure (select statement with joins) What I'm trying to do is calculate total work. My situation: I have 3 tables I'm using -Input (char...
5
by: Guoqi Zheng | last post by:
Dear sir, I am about to finish a new asp.net application, I used a lot of ms sql stored procedure. Those stored procudure I think is the most important part of the application. Now I am about...
5
by: Wael | last post by:
Hi, I have the following stored procedure that does some processing and puts the result in a temporary table. I tried several things that procedure to display output that I can access with...
0
by: zoom14151 | last post by:
I have a asp.net web page that uses a Crystal report. The report gets its data from a stored procedure that has three parameters @Startdate,@enddate and @teams. The page passes @Teams with multi...
1
by: hellboss | last post by:
Hi to one & all ! I need to update multiple tables with a single stored procedure, is this possible added can we use the alaias or joins in update query ??? my query was looking like this ...
2
by: wjunk | last post by:
Hi, basically i've ben searching forums and google with no success with my problem.... I've got a stored procedure which returns a select query on a linked server. This works fine if i call...
5
by: cardscat | last post by:
I am having a problem with a DB2 stored Procedure written in COBOL. This sp takes several input parameters of different types: Linkage Section. 01 PARMDEC1 PIC S9(5) COMP-3. 01 ...
1
by: sweatha | last post by:
Hi I have to convert the following query into stored procedure "select DoctorMaster.FirstName+''+DoctorMaster.MiddleName+''+DoctorMaster.LastName as...
2
by: kxyz | last post by:
Hello everyone, I need help with a stored procedure or two. My stored procedures are supposed to check if a certain record exists. If it does exist, then I select everything from that row, as...
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
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.