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 > How to update first n Rows?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-09, 01:51
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
How to update first n Rows?

How to update first n Rows?

Update TABLE1 SET FLAG='R' WHERE FLAG='N';

FLAG 'N' Values have thousands of records.

i want to update only first 15 rows.

if 15 records contains 'R' i should not update as 'R'

If FLAG 'R' is less than 15 records then update as 'R'(if 7 records already contains 'R" then i need Update only 8 records as 'R')

Last edited by laknar; 04-21-09 at 01:55.
Reply With Quote
  #2 (permalink)  
Old 04-21-09, 06:00
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
create a stored procedure - count the records updated and stop when enough
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 04-21-09, 08:39
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What are the first 15 rows for you? Any row?

You could also do that (untested):
Code:
UPDATE ( SELECT * FROM table1 WHERE flag = 'N' FETCH FIRST 15 ROWS ONLY )
SET flag = 'R'
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 04-21-09, 08:58
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I added "AS R" to make the statement work on DB2 V9.1 or earlier.

Change <primary key> to the name of the primary key(or a unique column) of your TABLE1.

Code:
UPDATE
       TABLE1
   SET flag = 'R'
 WHERE <primary key> IN
     ( SELECT <primary key>
         FROM
            ( SELECT <primary key>
                   , ROWNUMBER() OVER() rn
                FROM TABLE1
               WHERE flag = 'N'
            ) AS R
        WHERE rn <= 15
                  - (SELECT COUNT(*)
                       FROM TABLE1
                      WHERE flag = 'R')
     )
;

Last edited by tonkuma; 04-21-09 at 09:15.
Reply With Quote
  #5 (permalink)  
Old 03-27-12, 10:13
samibhashani samibhashani is offline
Registered User
 
Join Date: Mar 2012
Posts: 1
...................
Reply With Quote
  #6 (permalink)  
Old 03-27-12, 11:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question

Quote:
Originally Posted by tonkuma View Post
I added "AS R" to make the statement work on DB2 V9.1 or earlier.

Change <primary key> to the name of the primary key(or a unique column) of your TABLE1.

Code:
UPDATE
       TABLE1
   SET flag = 'R'
 WHERE <primary key> IN
     ( SELECT <primary key>
         FROM
            ( SELECT <primary key>
                   , ROWNUMBER() OVER() rn
                FROM TABLE1
               WHERE flag = 'N'
            ) AS R
        WHERE rn <= 15
                  - (SELECT COUNT(*)
                       FROM TABLE1
                      WHERE flag = 'R')
     )
;
Tonkuma, I believe the number:

Code:
15 - (SELECT COUNT(*)
          FROM TABLE1
         WHERE flag = 'R')
Could be negative because of COUNT(*) you'll get on whole table.

laknar, how somebody can understand it was updated 'R' to 'R', or not.

Maybe somebody use TRIGGER ?


Lenny
Reply With Quote
  #7 (permalink)  
Old 03-27-12, 12:55
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation Tested Solution

If you have UNIQUE_ID on the table you can run the query:

Code:
Update TABLE1 SET FLAG = 'R' 
Where unique_id in 
(select unique_id from TABLE1 
  order by 1
  fetch first 15 rows only)
And FLAG = 'N' 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If not, you can use the unique combination the columns of the TABLE1:

Code:
Update TABLE1 SET FLAG = 'R' 
Where (clmni1, clmni2,..., clmnik) in 
(select clmni1, clmni2,..., clmnik from TABLE1 
  order by 1, 2, ..., k
  fetch first 15 rows only)
And FLAG = 'N' 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
This code has to work.
I have tried it on my test table with perfect result.

Lenny
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