473,508 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to UPDATE using ROWNUM and ORDER BY

I am trying to do the following:

EXEC SQL
UPDATE MY TABLE
SET COL1 = :newValue
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2;

(index on COL1, COL2)

Pro*C does not process the "ORDER BY" statement.

How can I achieve the above

Thanks
Sandra
Jul 19 '05 #1
9 98763
sa*********@yahoo.com (Sandy) wrote:
I am trying to do the following:

EXEC SQL
UPDATE MY TABLE
SET COL1 = :newValue
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2;

(index on COL1, COL2)

Pro*C does not process the "ORDER BY" statement.

How can I achieve the above

Thanks
Sandra


Why are you using an ORDER BY or ROWNUM in an update statement?

What are you attempting to accomplish?
Jul 19 '05 #2
sa*********@yahoo.com (Sandy) wrote in message news:<f2*************************@posting.google.c om>...
I am trying to do the following:

EXEC SQL
UPDATE MY TABLE
SET COL1 = :newValue
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2;

(index on COL1, COL2)

Pro*C does not process the "ORDER BY" statement.

How can I achieve the above

Thanks
Sandra


"order by" is not valid in an update command.
It appears that what you want to do is to update the row having the
lowest value for col2 where col1=0. You could use this:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE col1 = 0);

Of course if the table is very large and there is no index on col2,
this could be slow. Another alternative is to use a PL/SQL block.

DECLARE
CURSOR c IS
SELECT col1 FROM my_table
WHERE col1 = 0
ORDER BY col2
FOR UPDATE OF col1;
r c%ROWTYPE;
BEGIN
OPEN c;
FETCH c INTO r;
IF c%FOUND THEN
UPDATE my_table SET col1 = :newValue
WHERE CURRENT OF c;
END IF;
CLOSE c;
END;

An advantage here is that if the min(col2) value is in multiple rows
with col1 = 0, the first example will update all of them while the
second example will only update one. No way to tell which one though.

Hope this helps
Ken Denny
Jul 19 '05 #3
UPDATE MY_TABLE
SET COL1 = :newValue
WHERE COL1 = 0
and col2 = ( select min(col2) from my_table )

Should do it

Pratap
Jul 19 '05 #4
sa*********@yahoo.com (Sandy) wrote in message news:<f2*************************@posting.google.c om>...
I am trying to do the following:

EXEC SQL
UPDATE MY TABLE
SET COL1 = :newValue
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2;

(index on COL1, COL2)

Pro*C does not process the "ORDER BY" statement.

How can I achieve the above

Thanks
Sandra


If the above worked you would be updating an unpredictable row in the
database. Why would you wish to do this.

Niall Litchfield
Oracle DBA
http://www.niall.litchfield.dial.pipex.com
Jul 19 '05 #5
Thank you all for your help, I have one further question.
It appears that what you want to do is to update the row having the
lowest value for col2 where col1=0. You could use this:
UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE col1 = 0);


Yes, That is what I was trying do and only update a single row (in one statement).

Could I do something like this:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1 AND col1 = 0);

If this is OK, I was wondering what are the performance implications
of moving the "WHERE" clause to the outer update statement (index is
on col2 as well), i.e.:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1);

Thanks
Sandra
Jul 19 '05 #6
GQ
sa*********@yahoo.com (Sandy) wrote in message news:<f2**************************@posting.google. com>...
Thank you all for your help, I have one further question.
It appears that what you want to do is to update the row having the
lowest value for col2 where col1=0. You could use this:
UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE col1 = 0);


Yes, That is what I was trying do and only update a single row (in one statement).

Could I do something like this:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1 AND col1 = 0);

If this is OK, I was wondering what are the performance implications
of moving the "WHERE" clause to the outer update statement (index is
on col2 as well), i.e.:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1);

Thanks
Sandra


1- Rownum doesn't buy you anything, other then ending the inner select
after retreiving one row. That row can be any row within your table -
due to the random retreival by Oracle (will probably be the first
physical row in the table, most of the time).
With that - returning only one row - why request the min(col2) - since
you only have one row ?

2- What would almost make more sense is moving the 'rownum=1'
condition to the outer SQL statement (update), but there again you are
selectively picking the first row, when you don't know the order that
multiple rows may be returned.

FYI - rownum is not a row id and can not be counted on to identify a
specific row, other then the position that the row is returned with
the return set.
Furthermore, if you don't specify an 'order by clause' the rows can be
in any order.
Jul 19 '05 #7
sa*********@yahoo.com (Sandy) wrote in message news:<f2**************************@posting.google. com>...
Thank you all for your help, I have one further question.
It appears that what you want to do is to update the row having the
lowest value for col2 where col1=0. You could use this:
UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE col1 = 0);


Yes, That is what I was trying do and only update a single row (in one statement).

Could I do something like this:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1 AND col1 = 0);

If this is OK, I was wondering what are the performance implications
of moving the "WHERE" clause to the outer update statement (index is
on col2 as well), i.e.:

UPDATE MY_TABLE
SET col1 = :newValue
WHERE col1 = 0
AND col2 =
(SELECT MIN(col2) FROM MY_TABLE WHERE ROWNUM = 1);

Thanks
Sandra


You need the "where col1 = 0" both places. This will work:

UPDATE my_table
set col1 = :newValue
where rowid =
(select rowid from my_table
where col1 = 0
and col2 =
(select min(col2) from my_table
where col1 = 0)
and rownum = 1);

Ken
Jul 19 '05 #8
UPDATE MY_TABLE
SET COL1 = :NEWVALUE
WHERE ROWID =
(SELECT ROWID
FROM MY_TABLE
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2)
Jul 19 '05 #9
kk****@hotmail.com (Kevin) wrote in message news:<5a**************************@posting.google. com>...
UPDATE MY_TABLE
SET COL1 = :NEWVALUE
WHERE ROWID =
(SELECT ROWID
FROM MY_TABLE
WHERE COL1 = 0
AND ROWNUM = 1
ORDER BY COL2)


Kevin,

Since rhe pseudo-column ROWNUM is assigned BEFORE any ORDER BY clause,
the above query does not do what you seem to think it does.

<rant>
And my standard question about use of ROWNUM applies:
WHY USE IT?

If you really have duplicate rows (all columns identical values), then
removing all but one seems to be better than faking differences in
rows based on the arbtrary order of retrieval. If they aren't really
duplicate rows, then use the other columns that aren't identical to
correctly sort out the ONE row you really want. (then get a proper
primary key created on that table ASAP)

But if you really must do something like this, what's wrong with a
little PL/SQL block? (and don't forget the FIRST_ROWS hint to the
optimizer)
</rant>

Sorry, but I just really dislike ROWNUM and it's abuses.
Ed
Jul 19 '05 #10

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

Similar topics

0
3079
by: Ed | last post by:
------=_NextPart_000_001D_01C34FCC.1D2B5E50 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable There seems to be a significant penalty imposed by the...
4
2339
by: MaRcElO PeReIrA | last post by:
Hi there, I was in troubles with a UPDATE+IN statement: The following command use to take about 5 minutes to be done: UPDATE requisicao SET conclusao='3' WHERE reg IN (SELECT reg FROM...
1
1628
by: jagdishl | last post by:
Hi: I have used the following code for my update query button I have used VB.Net Private Sub btncollections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
4
3381
by: Jim Hammond | last post by:
It would be udeful to be able to get the current on-screen values from a FormView that is databound to an ObjectDataSource by using a callback instead of a postback. For example: public void...
4
9573
by: fmatamoros | last post by:
I sometimes get the following error from an update statement in a stored procedure: Transaction (Process ID 62) was deadlocked on thread | communication buffer resources with another process and...
0
2179
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
2
10436
by: Godzilla | last post by:
Dear all, I have a challenge in hand and am not too sure how to accomplish this using stored procedure. I have a table containing about 3 fields, but I need to reorder/renumber a field value...
1
11866
by: tekedge | last post by:
Hi, I have to do an update using a query which uses CTE . Could any body please tell me how I can do an update. I am unable to update if I replace the final select with an update one. Thanks...
0
8226
by: AmitIper | last post by:
This Post is used to show how to bulk update records from grid view rather to updating one by one. The concept : create a xml string(string will contain all the records to be updated with some...
0
7229
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
7333
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,...
0
7398
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...
1
7061
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
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4716
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...
0
3208
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...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.