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

sql stored procedure questions

I am trying to create a stored procedure with following functionality.
I need to select a table where status = 'G' and return the row to the
application and then update all those rows that I just selected to
change status to 'P'. This table is being inserted all the time.

can i do this :

create a procedure sp1
....
RESULT SETS 1
....
declare cursor c1 with return for
select ....
from tb1
where status = 'G' for update of status
....
....
open c1
fetch c1 into outvariable
while .. do
update tb1 set status = 'P'
where current of c1
fetch c1 into outvariable
end while
....

Will this work ? or is there any other way to accomplish this.

Thanks

Jul 25 '06 #1
7 1911
Looking at your pseudocode, I believe that you can absolutely do this
in an SP, but you might want also to consider a more esoteric feature
of DB2: modiying table functions, to wit:

SELECT
*
FROM
OLD TABLE
(
UPDATE
TB1
SET
STATUS = 'P
WHERE
STATUS = 'G'
)

This allows you to 1) update the value, and 2) return the "before
image" of the data. This should be markedly faster than a procedural
implementation. Let us know if this fits the bill.

Further reading: www.vldb.org/conf/2004/IND1P1.PDF

--Jeff

Roger wrote:
I am trying to create a stored procedure with following functionality.
I need to select a table where status = 'G' and return the row to the
application and then update all those rows that I just selected to
change status to 'P'. This table is being inserted all the time.

can i do this :

create a procedure sp1
...
RESULT SETS 1
...
declare cursor c1 with return for
select ....
from tb1
where status = 'G' for update of status
...
...
open c1
fetch c1 into outvariable
while .. do
update tb1 set status = 'P'
where current of c1
fetch c1 into outvariable
end while
...

Will this work ? or is there any other way to accomplish this.

Thanks
Jul 25 '06 #2
Roger wrote:
I am trying to create a stored procedure with following functionality.
I need to select a table where status = 'G' and return the row to the
application and then update all those rows that I just selected to
change status to 'P'. This table is being inserted all the time.

can i do this :

create a procedure sp1
...
RESULT SETS 1
...
declare cursor c1 with return for
select ....
from tb1
where status = 'G' for update of status
...
...
open c1
fetch c1 into outvariable
while .. do
update tb1 set status = 'P'
where current of c1
fetch c1 into outvariable
end while
...

Will this work ? or is there any other way to accomplish this.

Thanks

No reason to FETCH. The stored PROCEDURE can RETURN the entire CURSOR
to the CLIENT, if it is OPENed before the PROCEDURE ends.

Here's an example:

CREATE PROCEDURE Example()
BEGIN
DECLARE List CURSOR WITH RETURN TO CLIENT FOR SELECT * FROM MyTable;
OPEN List;
END

B.

Jul 26 '06 #3
Thanks for the input...but brian, I need to update the rows i selected.
Thats why I tried to fetch and update the rows with current of. That
does not seem to work.

Thanks

Brian Tkatch wrote:
Roger wrote:
I am trying to create a stored procedure with following functionality.
I need to select a table where status = 'G' and return the row to the
application and then update all those rows that I just selected to
change status to 'P'. This table is being inserted all the time.

can i do this :

create a procedure sp1
...
RESULT SETS 1
...
declare cursor c1 with return for
select ....
from tb1
where status = 'G' for update of status
...
...
open c1
fetch c1 into outvariable
while .. do
update tb1 set status = 'P'
where current of c1
fetch c1 into outvariable
end while
...

Will this work ? or is there any other way to accomplish this.

Thanks


No reason to FETCH. The stored PROCEDURE can RETURN the entire CURSOR
to the CLIENT, if it is OPENed before the PROCEDURE ends.

Here's an example:

CREATE PROCEDURE Example()
BEGIN
DECLARE List CURSOR WITH RETURN TO CLIENT FOR SELECT * FROM MyTable;
OPEN List;
END

B.
Jul 26 '06 #4
Use Jeff's SELECT FROM UPDATE proposal as the cursor.
DB2 will update the rows and pass you back the changes as a result set.

DECLARE cur CURSOR WITH RETURN FOR SELECT ... FROM OLD TABLE(UPDATE ...);
OPEN cur;

... which of course begs the question why you bother with the procedure,
but that's performance.. next chapter.

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

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 26 '06 #5

Roger wrote:
Thanks for the input...but brian, I need to update the rows i selected.
Thats why I tried to fetch and update the rows with current of. That
does not seem to work.

Thanks

Brian Tkatch wrote:
Roger wrote:
I am trying to create a stored procedure with following functionality.
I need to select a table where status = 'G' and return the row to the
application and then update all those rows that I just selected to
change status to 'P'. This table is being inserted all the time.
>
can i do this :
>
create a procedure sp1
...
RESULT SETS 1
...
declare cursor c1 with return for
select ....
from tb1
where status = 'G' for update of status
...
...
open c1
fetch c1 into outvariable
while .. do
update tb1 set status = 'P'
where current of c1
fetch c1 into outvariable
end while
...
>
Will this work ? or is there any other way to accomplish this.
>
Thanks

No reason to FETCH. The stored PROCEDURE can RETURN the entire CURSOR
to the CLIENT, if it is OPENed before the PROCEDURE ends.

Here's an example:

CREATE PROCEDURE Example()
BEGIN
DECLARE List CURSOR WITH RETURN TO CLIENT FOR SELECT * FROM MyTable;
OPEN List;
END

B.
Oh. I just figured you could run the UPDATE first.

B.

Jul 26 '06 #6
OK...may be I should have mentioned I am trying to execute this
procedure in DB2 V 7 on Z/os. The select from old table (update...)
doesn't work in Db2 v7 on mainframe.

Any suggestions...

Thanks'
Serge Rielau wrote:
Use Jeff's SELECT FROM UPDATE proposal as the cursor.
DB2 will update the rows and pass you back the changes as a result set.

DECLARE cur CURSOR WITH RETURN FOR SELECT ... FROM OLD TABLE(UPDATE ...);
OPEN cur;

.. which of course begs the question why you bother with the procedure,
but that's performance.. next chapter.

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

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 26 '06 #7
Roger wrote:
OK...may be I should have mentioned I am trying to execute this
procedure in DB2 V 7 on Z/os. The select from old table (update...)
doesn't work in Db2 v7 on mainframe.
True.. that should be in DB2 9 for zOS.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Jul 26 '06 #8

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

Similar topics

15
by: Jarrod Morrison | last post by:
Hi All Im generally a vb programmer and am used to referencing multiple records returned from a query performed on an sql database and im trying to move some functions of my software into sql...
18
by: Jarrod Morrison | last post by:
Hi All I was wondering if there is a way to call a stored procedure from inside another stored procedure. So for example my first procedure will call a second stored procedure which when...
3
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm...
5
by: Raquel | last post by:
This is a very simple DB2 SQLJ stored procedure. The problem is that it seems to run fine but returns NOTHING. I mean..as if nothing has happened..not resultset is returned. I am passing value...
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: kanda | last post by:
Hello. I am developing the application (VBA&ODBC, to be exact) which periodically calls the stored procedures in the IBM DB2. A few of the procedures require executing with isolation level RR (...
1
by: E.T. Grey | last post by:
Hi All, Despite spending the past six to seven hours perusing the docs on the mySQl site, I still have questions unanswered, and have been unable to get any help. I am familiar with Sybase, some...
1
by: E.T. Grey | last post by:
I have been busting my nut over this for pretty much most of the day and it is driving me nuts. I posted this to an mySQL ng yesterday and I have not had any response (I'm pulling my hair out...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.