473,797 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3702
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
4524
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 procedures that in fact do run fine through my web application though, so I do not believe its a front-end problem. The procedures only take about 30 seconds to run from the back-end, so I know its not a time out issue as well. Does anyone have...
7
2273
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, table design, etc. I have a couple of questions, however. First, stored procedures vs. functions. In my world, a function is a body of code that returns a value; a procedure is a body of code that does things but does not return a value (other than...
5
4535
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 fenced sql procedures that have been excuting o.k. We implemented a fenced stored procedure on a C program and when trying to execute it, we get the "DIA3833C The system memory limit was reached."
2
9246
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 problems doing this. I wanted to implemented a generic "Helper" class like this: /** * Helper
2
1248
by: vj | last post by:
Please advice me with a few comparison facts of SQL Procedures and External Procedures ( c or java ) . -Vj
5
3487
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 Procedures. I thought stored pricedures were an Oracle/MS SQL Server thing and don't know how they work with Access Jet. I've looked at some of the help on stored procedures in A2003, but really don't understand what's going on. Can someone...
2
2353
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 with nested procedures. That is, a procedure (or function) can contain local procedures (and functions). Procedures can be nested arbitrarily deep. The scoping rules for procedure names (i.e., the ability to call
3
3645
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 page, 2) Breakpoint in the Page_Load, 3) debug the web page and click the submit button, 4) "step into" under debug several times, 5) The debugger does not stop at any of the statements in the click event handler. A breakpoint is needed in each...
28
72569
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 2005 Test Environments. What is the purpose of a view if I can just copy the vode from a view and put it into a stored procedure? Should I be accessing views from stored procedures?
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7560
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.