473,406 Members | 2,816 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.

JDBC: problems using FETCH FIRST and FOR UPDATE clause at the same time.

Hi,
I am developing a JDBC application and I encountered this problem (DB2
for ISeries).
I want to do a select for update and also use the fetch first rows clause.
This is my sql statement:

SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte FROM
USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY FOR UPDATE

This runs fine (the cursor gets this name 'P00022'), but when I execute the
update where current of:

UPDATE USERS SET UsrPwd=?, UsrPwdChgD=?, UsrEmail=?, UsrChgUsrI=?,
UsrChgDte=? WHERE CURRENT OF P00022

It throws the following error:
'Cursor P00022 for file GUSBRO1 read-only.'

If I take off the 'FETCH FIRST 1 ROWS' from the SELECT CLAUSE the update
works fine.

Is there a way to use both the FETCH FIRST 1 ROWS ONLY and the FOR UPDATE
clauses and still do the update ?

My DB version:
DB2 UDB for AS/400 version 05.01.0000 V5R1m0
My JDBC driver:
AS/400 Toolbox for Java JDBC Driver version 5.0

Regards,
Gustavo
Jan 2 '06 #1
9 11046
Acupuncture wrote:
SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte FROM
USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY FOR UPDATE
[...]
It throws the following error:
'Cursor P00022 for file GUSBRO1 read-only.'


From the iSeries UDB reference:

"Specification of the fetch-first-clause in a select-statement makes the
result table read-only. A read-only result table must not be referred to
in an UPDATE or DELETE statement. The fetch-first-clause cannot appear
in a statement containing an UPDATE clause."

I found an answer but I don't know a solution for you :-(

Bernd
--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Jan 2 '06 #2
Instead of FETCH FIRST in the cursor, try using ROW_NUMBER() OVER()
like

SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte
FROM (
SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte,
ROW_NUMBER() OVER() rn
FROM USERS WHERE UsrNbr = ?
) x WHERE rn = 1 FOR UPDATE

I am not sure though if iSerires supports that.

Regards,
-Eugene

Jan 2 '06 #3
Bernd Hohmann wrote:
Acupuncture wrote:
SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte
FROM USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY FOR UPDATE

[...]
It throws the following error:
'Cursor P00022 for file GUSBRO1 read-only.'

From the iSeries UDB reference:

"Specification of the fetch-first-clause in a select-statement makes the
result table read-only. A read-only result table must not be referred to
in an UPDATE or DELETE statement. The fetch-first-clause cannot appear
in a statement containing an UPDATE clause."

I found an answer but I don't know a solution for you :-(

The only thought I have on this is to use a searched update instead of
a cursor update.
Or you leave away the FETCH FIRST 1 ROW and simply close the cursor
after the single update.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 3 '06 #4
Serge is right, you could try something like this (update a view) in
one single statement:

UPDATE (
SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte
FROM
USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY
) x
SET x.UsrPwd=?, x.UsrPwdChgD=?, x.UsrEmail=?, x.UsrChgUsrI=?,
x.UsrChgDte=?

-Eugene

Jan 3 '06 #5
Eugene F wrote:
Serge is right, you could try something like this (update a view) in
one single statement:

UPDATE (
SELECT UsrNbr, UsrPwd, UsrPwdChgD, UsrEmail, UsrChgUsrI, UsrChgDte
FROM
USERS WHERE UsrNbr = ? FETCH FIRST 1 ROWS ONLY
) x
SET x.UsrPwd=?, x.UsrPwdChgD=?, x.UsrEmail=?, x.UsrChgUsrI=?,
x.UsrChgDte=?

-Eugene

Uh.. that's not what I meant... I'm quite certian that this does not
work in DB2 iSeries.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 3 '06 #6
No for the "for update statement". It won't work because the "fetch
first" turns it into a read only statement.

One question I have is, what is the primary key to the table and can
the where statement be quailified with this key. If so you will always
only get back one row which you can use the "for update statement on".

Jan 6 '06 #7
This one is working fine on V8.2.3 LUW:

db2 => create table t(c1 int, c2 varchar(10))
DB20000I The SQL command completed successfully.
db2 =>
db2 => insert into t values(1, '111'), (2, '222'), (4, '444'), (3,
'333')
DB20000I The SQL command completed successfully.
db2 =>
DB20000I The SQL command completed successfully.
db2 => select * from t

C1 C2
----------- ----------
1 111
2 222
4 444
3 333

4 record(s) selected.

db2 => update (select c2 from t where c1 > 2 fetch first 1 row only) x
set x.c2 = '000'
DB20000I The SQL command completed successfully.
db2 =>
db2 => select * from t

C1 C2
----------- ----------
1 111
2 222
4 000 <---- fetch first 1 row only
3 333

4 record(s) selected.

So it allowed to do the searched update with fetch first rows. Don't
see why it shouldn't in a proc.

-Eugene

Jan 6 '06 #8
Oh... sorry... it, of course, may not work on iSeries, wich I can't
test but maybe someone could run that tiny test on iSeries and let us
know :-))

Regards,
-Eugene

Jan 7 '06 #9
Eugene F wrote:
Oh... sorry... it, of course, may not work on iSeries, wich I can't
test but maybe someone could run that tiny test on iSeries and let us
know :-))

Regards,
-Eugene

I am quite confident thsi test will not work on iSeries.
In DB2 for LUW was introduced only in DB2 V8.1.4.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Jan 8 '06 #10

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

Similar topics

1
by: Howie Goodell | last post by:
Hello -- I am trying to optimise a JDBC connection and an Oracle 9i database for reading millions of records at a time to a 1 Gig PC running Win2K or XP. Two questions: 1. Does anyone have...
1
by: bogachkov | last post by:
Hello Joe Over the past several years, I have found your responses to jdbc usage/driver related issues to be extremely helpful. I am sure that you're very busy so I will make my question as...
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: Michel Esber | last post by:
System scenario: Linux DB2 Workgroup Server V7 FixPack 13. I have a java application (JDBC v2 driver) that reads into memory a considerable amount of data (100k-300k rows), summarizes the...
5
by: Bernd Hohmann | last post by:
Is there any way to create a dynamic row limit like "fetch first ? rows only" using the JDBC driver and a PreparedStatement? Bernd -- "Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch...
2
by: Suha Onay | last post by:
Hi, I have a problem with PostgreSQL 7.4. (With the old one 7.3 no probllem.) I create a connection and a statement : Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
8
by: Michel Esber | last post by:
Hello, Env: DB2 V8 LUW FP16 running Linux create Table X (machine_id varchar(24) not null, ctime timestamp not null); create index iFoo on X (MACHINE_ID, CTIME) allow reverse scans; alter...
1
by: shorti | last post by:
I am on DB2 UDB V8.2 When I try to do a simple update with a "Fetch First" clause I get a syntax error: db2 "update exec_1 set lock = 100, level = 50 where level = 0 fetch first 100 rows...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.