473,406 Members | 2,293 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,406 software developers and data experts.

Static cursors in DB2

Hello,

I need to know what's wrong in this translation from Informix. Note the
difference in the results, which is due to the UPDATE statement.

ORIGINAL PROGRAM (INFORMIX):
---------------------------
create table a(a int)
;
insert into a values(1)
;
insert into a values(1)
;
insert into a values(2)
;
create temp table c(a smallint)
;
create procedure pepe()
define b smallint;

foreach select a into b from a where a in(1,2)
update a set a = 0 where a = 1;
insert into c values(b);
end foreach;
end procedure
;
execute procedure pepe()
;
select * from c
;
drop procedure pepe
;
drop table a
;
drop table c
;

RESULTS:
-------
1
1
2
TRANSLATION TO DB2:
------------------
create table a (a integer)
@
insert into a values (1)
@
insert into a values (1)
@
insert into a values (2)
@
declare global temporary table session.c (a smallint) on commit
preserve rows
@
create procedure pepe()
begin
declare b smallint;
begin
declare f_foreach_pepe_1 integer default 0;
declare c_foreach_pepe_1 cursor for select a from a where a in (1,
2);
declare continue handler for not found set f_foreach_pepe_1 = 1;
open c_foreach_pepe_1;
fetch_loop_pepe_1:
loop
fetch c_foreach_pepe_1 into b;
if f_foreach_pepe_1 = 1 then leave fetch_loop_pepe_1; end if;
begin
update a set a = 0 where a = 1;
insert into session.c values (b);
end;
end loop;
close c_foreach_pepe_1;
end;
end
@
call pepe()
@
select * from session.c
@
drop procedure pepe
@
drop table a
@
drop table session.c
@

RESULTS:
-------
1
1

Nov 12 '05 #1
5 2183
Gustavo Randich wrote:
Hello,

I need to know what's wrong in this translation from Informix. Note the
difference in the results, which is due to the UPDATE statement.

ORIGINAL PROGRAM (INFORMIX):
---------------------------
create table a(a int)
;
insert into a values(1)
;
insert into a values(1)
;
insert into a values(2)
;
create temp table c(a smallint)
;
create procedure pepe()
define b smallint;

foreach select a into b from a where a in(1,2)
update a set a = 0 where a = 1;
insert into c values(b);
end foreach;
end procedure
;
execute procedure pepe()
;
select * from c
;
drop procedure pepe
;
drop table a
;
drop table c
;

RESULTS:
-------
1
1
2
TRANSLATION TO DB2:
------------------
create table a (a integer)
@
insert into a values (1)
@
insert into a values (1)
@
insert into a values (2)
@
declare global temporary table session.c (a smallint) on commit
preserve rows
@
create procedure pepe()
begin
declare b smallint;
begin
declare f_foreach_pepe_1 integer default 0;
declare c_foreach_pepe_1 cursor for select a from a where a in (1,
2);
declare continue handler for not found set f_foreach_pepe_1 = 1;
open c_foreach_pepe_1;
fetch_loop_pepe_1:
loop
fetch c_foreach_pepe_1 into b;
if f_foreach_pepe_1 = 1 then leave fetch_loop_pepe_1; end if;
begin
update a set a = 0 where a = 1;
insert into session.c values (b);
end;
end loop;
close c_foreach_pepe_1;
end;
end
@
call pepe()
@
select * from session.c
@
drop procedure pepe
@
drop table a
@
drop table session.c
@

RESULTS:
-------
1
1

First of: *wew* is this code generated by the MTK?
Here is what I would write:
create procedure pepe()
define b smallint;

for myrow AS select a from a where a in(1,2) do
update a set a = 0 where a = 1;
insert into c values(myrow.a);
end for;
end
%

Having said that here is the explanation for the behaviour.
The cursor over a is not explicitly declared as READ ONLY.
For that reason DB2 will fetch the rows one by one rather than fetching
rows in a batch to be able to support positioned UPDATE/DELETE (WHERE
CURRENT OF ).

So the cursor is actually affected by the update inside the loop which
nips its tail.
We call this "self hosing". No locking protects you there.
Now, I'm curious what would happen in IDS if you had, say, 10000 rows.
Will the entire resultset be buffered before the UPDATE happens, or will
IDS simply expose this behavior a bit later than DB2?

To the best of my knowledge enforcing materialization of resultsets from
cursors is not common for most RDBMS. It only happens as a side-effect
due to e.g. a SORT operation which is entirely optimizer dependent.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Ian
Serge Rielau wrote:

[snip]
We call this "self hosing". No locking protects you there.


"Self Hosing" -- that must the Toronto lab equivalent of "eating
your own dog food" :-)

Take off, eh?

Nov 12 '05 #3
Thank you Serge, you are always alert. It's very nice and clear the
theory, but below is the result of empirical research :)

- Using READ ONLY in the cursor declaration did not solve the problem
(try it).

- Using the "for myrow as select..." syntax you proposed solved the
problem, both with **AND WITHOUT** read-only declaration.

PD: I'm not using MTK, I'm using a home-made translator (using C, Lex,
Yacc, etc.). I've already programmed one for Informix->SQL Server and
one for Informix->Oracle, with full stored-procedure translation and
mimic of Informix behaviours. We code everything in Informix and use it
to port our projects automatically (tons of code).

Nov 12 '05 #4
Gustavo Randich wrote:
Thank you Serge, you are always alert. It's very nice and clear the
theory, but below is the result of empirical research :)

- Using READ ONLY in the cursor declaration did not solve the problem
(try it). Hmm, der may be more variables involved here.
- Using the "for myrow as select..." syntax you proposed solved the
problem, both with **AND WITHOUT** read-only declaration. Even if you extend your example to more rows? If yes I really want to
try it because that would be AI.
PD: I'm not using MTK, I'm using a home-made translator (using C, Lex,
Yacc, etc.). I've already programmed one for Informix->SQL Server and
one for Informix->Oracle, with full stored-procedure translation and
mimic of Informix behaviours. We code everything in Informix and use it
to port our projects automatically (tons of code).

... presumably not cheap. If you have improvement proposals for the MTK
feel free to send a note to mt*@us.ibm.com
The goal is not to burden customers with this.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Serge Rielau wrote:
- Using the "for myrow as select..." syntax you proposed solved the
problem, both with **AND WITHOUT** read-only declaration.
Even if you extend your example to more rows? If yes I really want to

try it because that would be AI. From over 4000 rows it begins to "lose" rows even with READ ONLY. To

try it I simply copy-paste the INSERTs to produce 4700+ rows of data.
In the meantime I'm not so worried because Informix 7.x loses rows at a
much greater rate! It seems like static cursors are an impossible
thing.

Nov 12 '05 #6

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

Similar topics

11
by: Alban Hertroys | last post by:
Oh no! It's me and transactions again :) I'm not really sure whether this is a limitation of psycopg or postgresql. When I use multiple cursors in a transaction, the records inserted at the...
22
by: T.S.Negi | last post by:
Hi All, I want to avoid using cursors and loops in stored procedures. Please suggest alternate solutions with example (if possible). Any suggestion in these regards will be appreciated. ...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
6
by: a | last post by:
Hello, I am doing some multithreading in an MDI app, and I can't seem to get the cursor to stay as an Hourglass. I call: Cursor.Current = cursors.wait at the beginning of my routing, and...
10
by: Just Me | last post by:
Does Me.Cursor.Current=Cursors.WaitCursor set the current property of Me.Cursor to Cursors.WaitCursor And Me.Cursor.Current=Cursors.Default set the Me.Current property to something (default)...
3
by: schwartzenberg | last post by:
Dear friends, I have just run into a strange DB2 problem. Something i'd some of you would answer, if only shortly. My basic question is: How do i ensure 'insensitive' (ie static) cursors...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
0
by: mmones | last post by:
Hello, I 'm working with an IBM DB2 V9 database via ODBC/CLI. I've got a problem with different lock behaviour in the following constellation / configuration. 1) connection C1 and connection...
1
by: Dima Kuchin | last post by:
Hello, I was trying to find the information about when and where should I use cursors in DB2, no luck. Maybe you can point me to some article that describes just that (or tell me which page is...
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: 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...
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
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...
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
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...

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.