473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update the first x of the records returned

1 New Member
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 1571
amitpatel66
2,367 Recognized Expert Top Contributor
Firstly, your update will not work if the sub query returns more than one order id.
Jun 9 '08 #2
r035198x
13,262 MVP
Is using a stored procedure an option?
Jun 9 '08 #3
subashsavji
93 New Member
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
3272
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 scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
4
4082
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 entire table or records returned, and updating a record if certain criteria is met. shouldn't the while loop do this? I know my syntax must be wrong, but difficult to work out how or where table = String(Request.Cookies("table"));
9
4360
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 predecessor, I hasten to add) so that each day it creates a copy of the record for each company, changes the date to today's date, and prompts the user for any changes of ratings on that day. The resulting data table grows by approx 600 records per...
1
455
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 RosterDate falls within a given date range, AgentID = 0 and FullName Is Null.
4
2286
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 the table until I can confirm that the record has in fact been deleted. This is obviously affecting my applications performance. What I need to do is something like :
5
2680
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) add_start_date is the newest date
1
5094
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: dsLocalDataSet.user_postRow newRow = dsLocalDataSet1.user_post.Newuser_postRow(); newRow.post_text = this.lblHiddenMessageStorage.Text; newRow.post_datetime = System.DateTime.Now; dsLocalDataSet1.user_post.Adduser_postRow(newRow);...
9
3287
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 of the records fields 4. pass those values to a function 5. write the value returned by the function the same record in a third field Hope this makes sense. Basically I want to read records and based on values
5
5610
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 uses very high volume DB transactions - in the order of tens of millions per day . . . Anyway, the current database which will remain nameless, but begins with O and rymes with debacle (sorta), has a problem with high volume work when it comes to...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10475
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.