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

Update the first x of the records returned

I need to update values in a table for a subset of rows returned from a select statement. The select statement may return any number of records. I'll use 15 in this example. I want to update the first 9 records returned with the value of "Green" and the remaining records with the value "Yellow". I need the data to be sorted in ascending order by the Order_ID field which is the unique key for the table. nGreen is a variable containing the maximum number of "Green" records.

I've tried something like this:

UPDATE TABLE SET COLOR = 'Green' WHERE ORDER_ID = (SELECT ORDER_ID FROM TABLE WHERE STATUS = 'Open' AND ROWNUM <= NGreen ORDER BY ORDER_ID ASC);

I would follow this with a second update statement to set the remaining records with "Yellow".

Thanks for your help!
Jun 7 '08 #1
3 1548
amitpatel66
2,367 Expert 2GB
Firstly, your update will not work if the sub query returns more than one order id.
Jun 9 '08 #2
r035198x
13,262 8TB
Is using a stored procedure an option?
Jun 9 '08 #3
may this be useful


DECLARE
A NUMBER;
CURSOR CUR1 IS
SELECT * FROM EMP WHERE DEPTNO=&D AND ENAME='&N'
FOR UPDATE OF SAL NOWAIT;
BEGIN
FOR I IN CUR1 LOOP
IF I.SAL > 3000 THEN
UPDATE EMP
SET SAL= I.SAL*1.2
WHERE CURRENT OF CUR1;
END IF;
A := I.SAL;
DBMS_OUTPUT.PUT_LINE(A);
END LOOP;
-- A := SAL;
--DBMS_OUTPUT.PUT_LINE(SAL);
END;
Jun 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
4
by: Roy Adams | last post by:
Hi posting again because no answer to previous.. tring to loop through a recordset and update a record, thing is it only updates the first record in the table rather than searching through the...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
1
by: Michelle | last post by:
Hi all I am having problems creating an update query. I have 2 tables, tblPublishRoster and tblCCAgents_Changed_Shifts. I want to select all records from tblCCAgents_ChangedShifts where...
4
by: Steve | last post by:
Hi I am having trouble with returning a value from an ado connection execute function when using msaccess. If I execute a statement to delete a record from a table I then have to loop through...
5
by: Darryl Kerkeslager | last post by:
How would I implement the WHER clause of this SQL? UPDATE address SET add_current=True WHERE (SELECT address_id, add_off_id, add_start_date FROM address as ATable WHERE count (add_off_id)...
1
by: Bennett Haselton | last post by:
Suppose I add a new row to a table in a dataset, and then I use an OleDbDataAdapter to add that new row to a SQL Server database using OleDbDataAdapter.Update(), as in the following code: ...
9
by: thomasp | last post by:
First of all, thanks for the help on my previous VB.NET/MS Access questions. This time I need do the following 1. Connect to a table 2. step through each of its records 3. read the value of two...
5
by: Louis LeBlanc | last post by:
Hey folks. I'm new to the list, and not quite what you'd call a DB Guru, so please be patient with me. I'm afraid the lead up here is a bit verbose . . . I am working on an application that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.