If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > FOR READ ONLY WITH UR in nested select

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-07, 16:13
HanbingL HanbingL is offline
Registered User
 
Join Date: Dec 2007
Posts: 15
FOR READ ONLY WITH UR in nested select

First of all, I am new to DB2 (3 weeks). I want to update a TEST table with PRODUCTION table in a nested select with read only with uncommitted read.

This is what I wanted to do but I got error message. Please help, thanks!

Code:
UPDATE TEST_STAFF S2
SET DEPT = (SELECT DEPT from PRODUCTION_STAFF
                   WHERE JOB = S2.JOB
                   ORDER BY DEPT
                   FETCH FIRST 1 ROWS ONLY
                   FOR READ ONLY WITH UR 
                 );
Reply With Quote
  #2 (permalink)  
Old 12-04-07, 16:45
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That is because the FOR READ ONLY and the WITH UR are not allowed in a full-select. They are only allowed in a select-statement.

Andy
Reply With Quote
  #3 (permalink)  
Old 12-04-07, 22:08
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
----------------------------------
Reply With Quote
  #4 (permalink)  
Old 12-05-07, 09:02
jsharon1248 jsharon1248 is offline
Registered User
 
Join Date: Apr 2007
Location: Chicago
Posts: 57
You're attempting to take a round-about way to get the lowest PRODUCTION_STAFF.DEPT value for a given TEST_STAFF.JOB. You'll be better off using a MIN function. Also, since you have the same column names on both tables, I'd recommend qualifying both tables for clarity.

Code:
UPDATE TEST_STAFF S2
SET DEPT = (SELECT MIN(P1.DEPT) from PRODUCTION_STAFF P1
                   WHERE P1.JOB = S2.JOB
                 );
Reply With Quote
  #5 (permalink)  
Old 12-05-07, 10:57
HanbingL HanbingL is offline
Registered User
 
Join Date: Dec 2007
Posts: 15
In SQL Server if I remember it correctly NOLOCK can be used in FULLSELECT. The example I showed is something I came up with. I can change it to ORDER BY ID or any order I needed.

My question is: Is there a way not to get the production table locked and to read the latest data in production.

Thanks.
Reply With Quote
  #6 (permalink)  
Old 12-05-07, 11:07
HanbingL HanbingL is offline
Registered User
 
Join Date: Dec 2007
Posts: 15
Never mind. I found a solution. http://publib.boulder.ibm.com/infoce...n/c0004121.htm
FYI:
Quote:
Uncommitted Read (UR)

For SELECT INTO, FETCH with a read-only cursor, fullselect in an INSERT, row fullselect in an UPDATE, or scalar fullselect (wherever it is used), the Uncommitted Read level allows:

* Any row read during a unit of work to be changed by other application processes.
* Any row changed by another application process to be read, even if the change has not been committed by that application process.
Code:
UPDATE TEST_STAFF S2
SET DEPT = (SELECT DEPT from PRODUCTION_STAFF
                   WHERE JOB = S2.JOB
                   ORDER BY SALARY
                   FETCH FIRST 1 ROWS ONLY                   
                 ) WITH UR ;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On