473,387 Members | 1,501 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.

DB2 UDB SQL Stored Procedure.

I'm having a tough time trying to run this stored procedure in DB2 UDB
8.1 environment. Can someone help me out here. All I'm trying to do
here is update an indicator 'N' if it is a NULL. Approximately 8
million rows will be updated as a result. I'm a newbie to DB2 and
writing my first SP. The problem is that Stored Proc doesnt seem to
update any rows eventhough I have set the commits after every row.
CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE v_POST_CD_ID INTEGER;
DECLARE v_CNT INTEGER DEFAULT 0;
DECLARE at_end INTEGER DEFAULT 0;
DECLARE at_close INTEGER DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE cur_open CONDITION FOR SQLSTATE '24501';
-- Declare cursor
DECLARE c1 CURSOR FOR
SELECT POST_CD_ID FROM tstdw.dwcrm.POST_CD WHERE POBOX_IND IS
NULL;

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE CONTINUE HANDLER FOR cur_open
SET at_close = 1;
-- Cursor left open for client application

OPEN c1;
upd_loop:
LOOP
-- IF at_close = 1 THEN
-- OPEN C1;
-- END IF;

FETCH c1 INTO v_POST_CD_ID;
SET v_CNT = v_CNT + 1;

IF at_end = 1 THEN
LEAVE upd_loop;
END IF;

update dwcrm.post_cd post set pobox_ind = 'N' where
POST_CD_ID=v_POST_CD_ID;

IF v_CNT = 500 THEN
SET v_CNT = 0;
COMMIT;
END IF;

END LOOP;

CLOSE c1;
END P1

Aug 24 '06 #1
4 9080
Kris, a sincere welcome to SP writing!

Looks like you're COMMITing at every 500th row, right? (if you wanted,
you could also just do a MOD by 500 on the counter instead of resetting
it). Anyway, I definitely wouldn't commit after every row, but if you
are going to commit, you should open your cursors WITH HOLD.

I'm not sure if the below code is the real code or just an example, but
I'm not sure I see the reason for a loop at all. It seems that you
could accomplish this as a straight update, unless you're REALLY
concerned with COMMITing every so often. Are you worried about
recoverability? Log-space usage?

Regards,

--Jeff

lo*********@gmail.com wrote:
I'm having a tough time trying to run this stored procedure in DB2 UDB
8.1 environment. Can someone help me out here. All I'm trying to do
here is update an indicator 'N' if it is a NULL. Approximately 8
million rows will be updated as a result. I'm a newbie to DB2 and
writing my first SP. The problem is that Stored Proc doesnt seem to
update any rows eventhough I have set the commits after every row.
CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE v_POST_CD_ID INTEGER;
DECLARE v_CNT INTEGER DEFAULT 0;
DECLARE at_end INTEGER DEFAULT 0;
DECLARE at_close INTEGER DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE cur_open CONDITION FOR SQLSTATE '24501';
-- Declare cursor
DECLARE c1 CURSOR FOR
SELECT POST_CD_ID FROM tstdw.dwcrm.POST_CD WHERE POBOX_IND IS
NULL;

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE CONTINUE HANDLER FOR cur_open
SET at_close = 1;
-- Cursor left open for client application

OPEN c1;
upd_loop:
LOOP
-- IF at_close = 1 THEN
-- OPEN C1;
-- END IF;

FETCH c1 INTO v_POST_CD_ID;
SET v_CNT = v_CNT + 1;

IF at_end = 1 THEN
LEAVE upd_loop;
END IF;

update dwcrm.post_cd post set pobox_ind = 'N' where
POST_CD_ID=v_POST_CD_ID;

IF v_CNT = 500 THEN
SET v_CNT = 0;
COMMIT;
END IF;

END LOOP;

CLOSE c1;
END P1
Aug 24 '06 #2
Jeff,
Thanks for your quick response. Yes I was attempting to commit every
500th row but it didnt work as well. The reason why I was forced to
write this proc is due to the fact my update statement fails as a
result of the transaction log getting filled. My DBA is not smart
enough to fix this issue so I decided to write a SP.

As is this SP should work right or is there any modifications that
needed to be made for it to function.

Thanks,
Lokesh

jefftyzzer wrote:
Kris, a sincere welcome to SP writing!

Looks like you're COMMITing at every 500th row, right? (if you wanted,
you could also just do a MOD by 500 on the counter instead of resetting
it). Anyway, I definitely wouldn't commit after every row, but if you
are going to commit, you should open your cursors WITH HOLD.

I'm not sure if the below code is the real code or just an example, but
I'm not sure I see the reason for a loop at all. It seems that you
could accomplish this as a straight update, unless you're REALLY
concerned with COMMITing every so often. Are you worried about
recoverability? Log-space usage?

Regards,

--Jeff

lo*********@gmail.com wrote:
I'm having a tough time trying to run this stored procedure in DB2 UDB
8.1 environment. Can someone help me out here. All I'm trying to do
here is update an indicator 'N' if it is a NULL. Approximately 8
million rows will be updated as a result. I'm a newbie to DB2 and
writing my first SP. The problem is that Stored Proc doesnt seem to
update any rows eventhough I have set the commits after every row.
CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE v_POST_CD_ID INTEGER;
DECLARE v_CNT INTEGER DEFAULT 0;
DECLARE at_end INTEGER DEFAULT 0;
DECLARE at_close INTEGER DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE cur_open CONDITION FOR SQLSTATE '24501';
-- Declare cursor
DECLARE c1 CURSOR FOR
SELECT POST_CD_ID FROM tstdw.dwcrm.POST_CD WHERE POBOX_IND IS
NULL;

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE CONTINUE HANDLER FOR cur_open
SET at_close = 1;
-- Cursor left open for client application

OPEN c1;
upd_loop:
LOOP
-- IF at_close = 1 THEN
-- OPEN C1;
-- END IF;

FETCH c1 INTO v_POST_CD_ID;
SET v_CNT = v_CNT + 1;

IF at_end = 1 THEN
LEAVE upd_loop;
END IF;

update dwcrm.post_cd post set pobox_ind = 'N' where
POST_CD_ID=v_POST_CD_ID;

IF v_CNT = 500 THEN
SET v_CNT = 0;
COMMIT;
END IF;

END LOOP;

CLOSE c1;
END P1
Aug 24 '06 #3
Kris, a sincere welcome to SP writing!

Looks like you're COMMITing at every 500th row, right? (if you wanted,
you could also just do a MOD by 500 on the counter instead of resetting
it). Anyway, I definitely wouldn't commit after every row, but if you
are going to commit, you should declare your cursor WITH HOLD.

I'm not sure if the below code is the real code or just an example, but
I don't readily see the reason for a loop at all--but if you go this
route, you might consider a cursor FOR loop with a positioned update.

It seems that you could accomplish this as a straight update, unless
you're REALLY concerned with COMMITing every so often. Are you worried
about recoverability? Log-space usage?

--Jeff

lo*********@gmail.com wrote:
I'm having a tough time trying to run this stored procedure in DB2 UDB
8.1 environment. Can someone help me out here. All I'm trying to do
here is update an indicator 'N' if it is a NULL. Approximately 8
million rows will be updated as a result. I'm a newbie to DB2 and
writing my first SP. The problem is that Stored Proc doesnt seem to
update any rows eventhough I have set the commits after every row.
CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN

DECLARE v_POST_CD_ID INTEGER;
DECLARE v_CNT INTEGER DEFAULT 0;
DECLARE at_end INTEGER DEFAULT 0;
DECLARE at_close INTEGER DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE cur_open CONDITION FOR SQLSTATE '24501';
-- Declare cursor
DECLARE c1 CURSOR FOR
SELECT POST_CD_ID FROM tstdw.dwcrm.POST_CD WHERE POBOX_IND IS
NULL;

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE CONTINUE HANDLER FOR cur_open
SET at_close = 1;
-- Cursor left open for client application

OPEN c1;
upd_loop:
LOOP
-- IF at_close = 1 THEN
-- OPEN C1;
-- END IF;

FETCH c1 INTO v_POST_CD_ID;
SET v_CNT = v_CNT + 1;

IF at_end = 1 THEN
LEAVE upd_loop;
END IF;

update dwcrm.post_cd post set pobox_ind = 'N' where
POST_CD_ID=v_POST_CD_ID;

IF v_CNT = 500 THEN
SET v_CNT = 0;
COMMIT;
END IF;

END LOOP;

CLOSE c1;
END P1
Aug 24 '06 #4
Lokesh:

Well--I had a 50/50 chance to get your name right and failed! :-)

Here's what I would do, more-or-less (note this code is UNTESTED!),
assuming DB2 UDB LUW. Note also that despite what I said earlier, you
*CANNOT* do a positioned update using a FOR loop. Sorry 'bout that!

CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
SPECIFIC POST_CD_UPD
INHERIT SPECIAL REGISTERS
BEGIN

DECLARE V_CNT INTEGER DEFAULT 0;--

FOR L_POST_CD AS C_POST_CD CURSOR WITH HOLD FOR
SELECT
POST_CD_ID
FROM
DWCRM.POST_CD
WHERE
POBOX_IND IS NULL

DO
UPDATE
DWCRM.POST_CD
SET
POBOX_IND = 'N'
WHERE
POST_CD_ID = L_POST_CD.POST_CD_ID;--

SET V_CNT = V_CNT + 1;--

IF
MOD(V_CNT,500) = 0
THEN
COMMIT;--
END IF;--

END FOR;--

END;

--Jeff

lo*********@gmail.com wrote:
Jeff,
Thanks for your quick response. Yes I was attempting to commit every
500th row but it didnt work as well. The reason why I was forced to
write this proc is due to the fact my update statement fails as a
result of the transaction log getting filled. My DBA is not smart
enough to fix this issue so I decided to write a SP.

As is this SP should work right or is there any modifications that
needed to be made for it to function.

Thanks,
Lokesh

jefftyzzer wrote:
Kris, a sincere welcome to SP writing!

Looks like you're COMMITing at every 500th row, right? (if you wanted,
you could also just do a MOD by 500 on the counter instead of resetting
it). Anyway, I definitely wouldn't commit after every row, but if you
are going to commit, you should open your cursors WITH HOLD.

I'm not sure if the below code is the real code or just an example, but
I'm not sure I see the reason for a loop at all. It seems that you
could accomplish this as a straight update, unless you're REALLY
concerned with COMMITing every so often. Are you worried about
recoverability? Log-space usage?

Regards,

--Jeff

lo*********@gmail.com wrote:
I'm having a tough time trying to run this stored procedure in DB2 UDB
8.1 environment. Can someone help me out here. All I'm trying to do
here is update an indicator 'N' if it is a NULL. Approximately 8
million rows will be updated as a result. I'm a newbie to DB2 and
writing my first SP. The problem is that Stored Proc doesnt seem to
update any rows eventhough I have set the commits after every row.
>
>
CREATE PROCEDURE DWCRM.POST_CD_UPD ()
LANGUAGE SQL
------------------------------------------------------------------------
-- SQL Stored Procedure
------------------------------------------------------------------------
P1: BEGIN
>
DECLARE v_POST_CD_ID INTEGER;
DECLARE v_CNT INTEGER DEFAULT 0;
DECLARE at_end INTEGER DEFAULT 0;
DECLARE at_close INTEGER DEFAULT 0;
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE cur_open CONDITION FOR SQLSTATE '24501';
-- Declare cursor
DECLARE c1 CURSOR FOR
SELECT POST_CD_ID FROM tstdw.dwcrm.POST_CD WHERE POBOX_IND IS
NULL;
>
DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;
DECLARE CONTINUE HANDLER FOR cur_open
SET at_close = 1;
-- Cursor left open for client application
>
OPEN c1;
upd_loop:
LOOP
-- IF at_close = 1 THEN
-- OPEN C1;
-- END IF;
>
FETCH c1 INTO v_POST_CD_ID;
SET v_CNT = v_CNT + 1;
>
IF at_end = 1 THEN
LEAVE upd_loop;
END IF;
>
update dwcrm.post_cd post set pobox_ind = 'N' where
POST_CD_ID=v_POST_CD_ID;
>
IF v_CNT = 500 THEN
SET v_CNT = 0;
COMMIT;
END IF;
>
END LOOP;
>
CLOSE c1;
END P1
Aug 24 '06 #5

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

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
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...
3
by: kd | last post by:
Hi All, How to debug a stored procedure? Thanks, kd
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
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: 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...
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...
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.