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

Stored Procedures / UDF

Hey

I am studying DB2 right know and trying to make some things to work. I
noticed that I might understood some things wrong. Could you help me
clarify a bit?

That's what I wanted to do: A function/procedure I can call in a SQL.
This function queries the database and returns a value. I thought I can
write a stored proc like...

CREATE PROCEDURE DB2ADMIN.getLoadId ( IN p_table_name CHAR(30)
, IN p_load_grp_id INTEGER
, OUT p_load_id INTEGER)
LANGUAGE SQL
SPECIFIC getLoadId
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
getId: BEGIN
SELECT load_info.load_id INTO p_load_id
FROM
load_info LEFT JOIN loads ON
load_info.load_id = loads.load_id
WHERE
load_grp_id = p_load_grp_id AND
load_table = p_table_name;
END getId

....and use this in a sql query like...

******
SELECT * FROM employee WHERE employee.load_info =
getLoadId('employee',1,?)
******

But obviously you cannot call a stored procedure in a SQL query?

Then I wanted to do the same stuff with a UDF but I read somewhere that
an external scalar function cannot query the database??
Now I am confused... Is there a way to do what I want?

Thank you...
Michael

Mar 17 '06 #1
3 1860
PROCEDURE take procedural action on the database, and as such cannot be
CALLed from within a SQL query. They must be CALLed with the CALL
keyword.

A UDF can be used in the query. I am not sure of the limitation on
which queries it can execute. I would try using one, as in:

CREATE FUNCTION getLoadId(p_table_name CHAR(30), p_load_grp_id INTEGER)
RETURNS INTEGER
RETURN SELECT load_info.load_id FROM
load_info LEFT JOIN loads ON
load_info.load_id = loads.load_id
WHERE
load_grp_id = p_load_grp_id AND
load_table = p_table_name

B.

Mar 17 '06 #2
Michael Rechberger wrote:
Hey

I am studying DB2 right know and trying to make some things to work. I
noticed that I might understood some things wrong. Could you help me
clarify a bit?

That's what I wanted to do: A function/procedure I can call in a SQL.
This function queries the database and returns a value. I thought I can
write a stored proc like...

CREATE PROCEDURE DB2ADMIN.getLoadId ( IN p_table_name CHAR(30)
, IN p_load_grp_id INTEGER
, OUT p_load_id INTEGER)
LANGUAGE SQL
SPECIFIC getLoadId
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
getId: BEGIN
SELECT load_info.load_id INTO p_load_id
FROM
load_info LEFT JOIN loads ON
load_info.load_id = loads.load_id
WHERE
load_grp_id = p_load_grp_id AND
load_table = p_table_name;
END getId

...and use this in a sql query like...

******
SELECT * FROM employee WHERE employee.load_info =
getLoadId('employee',1,?)
******

But obviously you cannot call a stored procedure in a SQL query?

Then I wanted to do the same stuff with a UDF but I read somewhere that
an external scalar function cannot query the database??


You can query the database. You can use a SQL-bodied function, i.e. one
declared with LANGUAGE SQL, or you use an external routine written in C/C++
or Java. Either way, queries can be used w/o any problem. Brian already
gave you a solution for a LANGUAGE SQL UDF.

If possible, I'd recommend to stick with SQL-bodied functions. They are
compiled into the SQL statement that invokes the function and, thus, gives
the optimizer a much better chance to optimize the overall query. This is
not possible with external code that can influence which (sub-)statements
are executed.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Mar 20 '06 #3
Thank you, that helped a lot, I realized it with a UDF similar to
Brian's.

Mar 24 '06 #4

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

Similar topics

11
by: jrefactors | last post by:
I want to know the differences between SQL Server 2000 stored procedures and oracle stored procedures? Do they have different syntax? The concept should be the same that the stored procedures...
2
by: scott | last post by:
Hi, Just wondering what sort of problems and advantages people have found using stored procedures. I have an app developed in VB6 & VB.NET and our developers are starting to re-write some of the...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: Rhino | last post by:
I am trying to determine the behaviour of stored procedures in DB2 V8.2.x in Windows/Unix/Linux and how I can control that behaviour. Some documentation in the manuals is confusing the issue...
5
by: Tim Marshall | last post by:
I was following the thread "Re: Access Treeview - Is it Safe Yet?" with interest and on reading the post describing Lauren Quantrell's SmartTree, I've run into something I don't understand: Stored...
2
by: Eli | last post by:
Hi all We currently have a strange problem with calling a Stored Procedure (SQL Database) in our C# Project. The only error I get is "System error" which says a lot :) Background: We have...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
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...
11
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
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:
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.