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

Sub procedures in DB2?

Hi all,

In Oracle stored procedures, you can declare sub-procedures to help
modularize your code... i.e.

CREATE OR REPLACE PROCEDURE myProcedure is

PROCEDURE b (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO bb
VALUES (field1, field2, field3);
...
END;

PROCEDURE c (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO cc
VALUES (field1, field2, field3);
...
END;

PROCEDURE a (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
AS
BEGIN
b(field1, field2, field3);
c(field1, field2, field3);
END;


What would a DB2 stored procedure look like if it followed the above
Oracle program logic?

Dec 8 '06 #1
5 3673
pa*************@gmail.com wrote:
Hi all,

In Oracle stored procedures, you can declare sub-procedures to help
modularize your code... i.e.

CREATE OR REPLACE PROCEDURE myProcedure is

PROCEDURE b (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO bb
VALUES (field1, field2, field3);
...
END;

PROCEDURE c (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO cc
VALUES (field1, field2, field3);
...
END;

PROCEDURE a (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
AS
BEGIN
b(field1, field2, field3);
c(field1, field2, field3);
END;


What would a DB2 stored procedure look like if it followed the above
Oracle program logic?
Just create the sub-procedures outside of the main procedure body.You
can place them in a separate schema not on the PATH to hide them if you
wish (somewhat similar to what you would do in Oracle by a package body).
I admit this is the very first time I see a subprocedure request.
Even in C/C++ this is not a popular feature....

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 8 '06 #2
Hi Serge,

Thanks for your reply. I think I know what you're talking about, but I
can't wrap my head around it. Do you mind providing a short code
example?

Currently my DB2 procedures look like this:

CREATE PROCEDURE myProcedure(...)_ IS

-- global variables here currently

-- put sub procedures here? i.e.
-- CREATE PROCEDURE B(...) IS BEGIN ... END;

P1: BEGIN
...
-- would like to call a sub procedure here i.e.
-- B(...)
...
END;

The problem is that the program I'm converting from is over 20k lines
on ONE file (insane) and they use global variables. The procedures in
the existing Oracle stored proc. modify these global variables so it's
a nightmare to convert to in DB2. Sometimes the sub-procedures simply
can't be put into another stored proc because they need to modify the
global variables, and that's what's stopping my progress.

On smaller scripts I'm currently using GOTO statements to control
execution flow with labels, but this is a hack solution that will not
work if there are multiple sub-procedure calls. The absolute worst case
scenario is I copy & paste the Oracle sub-procedures into places where
they are called, and that is so bad I don't want to think about it
right now. =)

Serge Rielau wrote:
pa*************@gmail.com wrote:
Hi all,

In Oracle stored procedures, you can declare sub-procedures to help
modularize your code... i.e.

CREATE OR REPLACE PROCEDURE myProcedure is

PROCEDURE b (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO bb
VALUES (field1, field2, field3);
...
END;

PROCEDURE c (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
IS
BEGIN
...
INSERT INTO cc
VALUES (field1, field2, field3);
...
END;

PROCEDURE a (field1 INTEGER,
field2 INTEGER,
field3 INTEGER)
AS
BEGIN
b(field1, field2, field3);
c(field1, field2, field3);
END;


What would a DB2 stored procedure look like if it followed the above
Oracle program logic?
Just create the sub-procedures outside of the main procedure body.You
can place them in a separate schema not on the PATH to hide them if you
wish (somewhat similar to what you would do in Oracle by a package body).
I admit this is the very first time I see a subprocedure request.
Even in C/C++ this is not a popular feature....

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 8 '06 #3
I see...
In DB2 for LUW today you have two options to cope with global variables:
1. Use a DECLAREd GLOBAL TEMPORARY TABLE.
That is declare a DGTT with one row and a column for each variable.
Then simply UPDATE the table instead of SET-ing the variables.
SELECT instead of reading it.
2. Do "the right thing" and pass variables back and forth through the
procedure as INOUT parameters.
This is the preferred way.

Example:
--#SET TERMINATOR !
CREATE PROCEDURE sub1(IN arg INTEGER, OUT res INTEGER,
INOUT globalvar INTEGER)
BEGIN
SET res = arg + 1;
SET globalvar = 5;
END
!

CREATE PROCEDURE sub2(IN arg INTEGER, OUT res INTEGER,
INOUT globalvar INTEGER)
BEGIN
SET res = arg - 1;
SET globalvar = 7;
END
!

CREATE PROCEDURE PROC(IN arg INTEGER, OUT res INTEGER)
BEGIN
DECLARE globalvar INTEGER;
CALL sub1(arg, arg, globalvar);
CALL sub1(arg, arg, globalvar);
SET res = arg + globalvar;
END
!

GRANT EXECUTE ON PROC(INTEGER, INTEGER) TO PUBLIC
!

--#SET TERMINATOR ;
Does that help?
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 8 '06 #4
Hi Serge,

This definitely is a workable solution.

One thing that concerns me is performance. Updating a global variable
say has a cpu/resource cost of 1, anyone remembers asymptotic analysis.
In the classroom, we're always told that constants and O(1) doesn't
matter, however that doesn't translate in the wonderful world of RDMS.

A function call also should have a cost of O(1). So assigning & reading
a global variable also has a cost of O(1), therefore they are
equivalent in this sense. But we all know that if you call a function >
20,000 times when you're processing 5 million records in 2 hours... it
adds up.

So my question is if I have to call these "helper" SPs all the time,
would that seriously impact the performance of the main SP?

In comparison, the script I'm running now in Oracle takes about 5 hours
to finish, and processes about 20 million records. I'm concerned that
with all this function calling, the script will take much much longer
and this would be unacceptable.

Any ideas on this would be appreciated!

- Patrick

Serge Rielau wrote:
I see...
In DB2 for LUW today you have two options to cope with global variables:
1. Use a DECLAREd GLOBAL TEMPORARY TABLE.
That is declare a DGTT with one row and a column for each variable.
Then simply UPDATE the table instead of SET-ing the variables.
SELECT instead of reading it.
2. Do "the right thing" and pass variables back and forth through the
procedure as INOUT parameters.
This is the preferred way.

Example:
--#SET TERMINATOR !
CREATE PROCEDURE sub1(IN arg INTEGER, OUT res INTEGER,
INOUT globalvar INTEGER)
BEGIN
SET res = arg + 1;
SET globalvar = 5;
END
!

CREATE PROCEDURE sub2(IN arg INTEGER, OUT res INTEGER,
INOUT globalvar INTEGER)
BEGIN
SET res = arg - 1;
SET globalvar = 7;
END
!

CREATE PROCEDURE PROC(IN arg INTEGER, OUT res INTEGER)
BEGIN
DECLARE globalvar INTEGER;
CALL sub1(arg, arg, globalvar);
CALL sub1(arg, arg, globalvar);
SET res = arg + globalvar;
END
!

GRANT EXECUTE ON PROC(INTEGER, INTEGER) TO PUBLIC
!

--#SET TERMINATOR ;
Does that help?
Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Dec 11 '06 #5
Patrick wrote:
Hi Serge,

This definitely is a workable solution.

One thing that concerns me is performance. Updating a global variable
say has a cpu/resource cost of 1, anyone remembers asymptotic analysis.
In the classroom, we're always told that constants and O(1) doesn't
matter, however that doesn't translate in the wonderful world of RDMS.

A function call also should have a cost of O(1). So assigning & reading
a global variable also has a cost of O(1), therefore they are
equivalent in this sense. But we all know that if you call a function >
20,000 times when you're processing 5 million records in 2 hours... it
adds up.

So my question is if I have to call these "helper" SPs all the time,
would that seriously impact the performance of the main SP?

In comparison, the script I'm running now in Oracle takes about 5 hours
to finish, and processes about 20 million records. I'm concerned that
with all this function calling, the script will take much much longer
and this would be unacceptable.

Any ideas on this would be appreciated!
I don't understand in your original you also have helper procedures.
I am not aware that Oracle derives any performance advantage out of
defining a procedure within a procedure vs. defining it in the same package.
The only difference wrt. the stored process I can see is that Oracle
loads the entire package into memory while in DB2 each procedure will be
loaded independently upon first usage.
There will be a small price to pay in DB2 by passing the global
variables around as argumnets, but assuming you ar enot pushing 2G LOBs
that shouldn't matter. All this is still O(1) btw. just a bigger 1 :-)

In my experience performance regression on migration is in it's first
order generated by the attempt to emulate the source DBMS on too high a
level. You will find sufficient codepath to squeeze out by optimizing
out the emulations. Small things like extra parameters or package level
caching play a second order role in performance at best.

E.g. look at nonsense casts due to VARCHAR2 semantics ('' == NULL).
DATE arithmetic is another juicy one.

search on www.ibm.com for "rielau" you will find an article on an SQL
Procedure tracer. My SQL Procedure Profiler is available for free and
supported in the Developer Workbench. You will find it invaluable to
track performance problems.

Cheers
Serge

PS: I sent you an off line note, did you get it? If not please ping me
with a viable email address.
Dec 11 '06 #6

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

Similar topics

6
by: Mike J | last post by:
I have several stored procedures that run fine from my SQL Server database (via the exec command.), though when I call these procedures from my web application, they do not complete. I have other...
7
by: BlueDragon | last post by:
The place where I work is moving to MS SQL Server from Lotus Notes. I have done a lot of coding in Lotus Notes, and have, I suppose, intermediate skills in basic SQL -- queries, insert, updates,...
5
by: Jeff | last post by:
I have question about differences in fenced sql procedures and fenced stored procedures. Do fenced sql procedures take up an extra memory segment when executed? Reason I ask is we have several...
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...
2
by: vj | last post by:
Please advice me with a few comparison facts of SQL Procedures and External Procedures ( c or java ) . -Vj
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: Quinnie | last post by:
Hi, I have a homework assignment that I'm so confused and really need help with. Here's the description, any help would be appreciated. Thanks! Assume we have a statically-scoped language...
3
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind...
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...
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.