How do you limit the number of rows updated

How do you limit the number of rows updated

Post by PB » Thu, 25 Oct 2001 01:01:00



I only want to update one row in a database.  However, my WHERE clause
affects two or more rows.  I cannot add any more conditions to the WHERE
clause to limit the number of rows affected.  This statement is for an Audit
type function which eliminates duplicate records that are created.

My SQL Statement:
UPDATE table SET columnx='1' WHERE id='1'

Is there a way to write an UPDATE SQL statement to limit the number of rows
that are updated?

 
 
 

How do you limit the number of rows updated

Post by BP Margoli » Thu, 25 Oct 2001 08:18:11


PB,

SET ROWCOUNT 1
UPDATE ...
SET ROWCOUNT 0

Keep in mind that which of the two rows will be updated is being left open
to "chance". That is not a completely accurate statement, because it is not
really chance, but the point being that while you will update only one row
of the table, depending upon a number of factors, SQL Server might one day
update row #1, and some other day update row #2.

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.


Quote:> I only want to update one row in a database.  However, my WHERE clause
> affects two or more rows.  I cannot add any more conditions to the WHERE
> clause to limit the number of rows affected.  This statement is for an
Audit
> type function which eliminates duplicate records that are created.

> My SQL Statement:
> UPDATE table SET columnx='1' WHERE id='1'

> Is there a way to write an UPDATE SQL statement to limit the number of
rows
> that are updated?


 
 
 

How do you limit the number of rows updated

Post by Frederik Jense » Thu, 25 Oct 2001 17:00:11



> Is there a way to write an UPDATE SQL statement to limit the number of
rows
> that are updated?

Yes, use a precise WHERE clause to limit the update to a (single) row.
--
Frederik Jensen, Judex
 
 
 

How do you limit the number of rows updated

Post by Nick » Fri, 26 Oct 2001 21:29:57


Make sure the id contains only unique ids (if you have '1'
in more than one row, more than one row will be updated).  
If the id column is not your primary key, you may consider
making it.  Also, I notice that in your sql statement you
have <snip>WHERE id = '1'<end>.  If the id column is
numeric, you're WHERE clause should look like this:

WHERE id = 1

No single quotes around the 1.

Quote:>-----Original Message-----
>I only want to update one row in a database.  However, my
WHERE clause
>affects two or more rows.  I cannot add any more

conditions to the WHERE
Quote:>clause to limit the number of rows affected.  This

statement is for an Audit
Quote:>type function which eliminates duplicate records that are
created.

>My SQL Statement:
>UPDATE table SET columnx='1' WHERE id='1'

>Is there a way to write an UPDATE SQL statement to limit
the number of rows
>that are updated?

>.

 
 
 

1. How do you limit the number of rows updated

I only want to update one row in a database.  However, my WHERE clause
affects two or more rows.  I cannot add any more conditions to the WHERE
clause to limit the number of rows affected.  This statement is for an Audit
type function which eliminates duplicate records that are created.

My SQL Statement:
UPDATE table SET columnx='1' WHERE id='1'

Is there a way to write an UPDATE SQL statement to limit the number of rows
that are updated?

2. Supported OS's

3. row numbers, limiting row# for web usage

4. Why my sorting order looks strange ?

5. Insert or update from n number of rows into a single row without a cursor

6. --A question for the pro's!!

7. row updating, trigger, row number or id, HELP

8. technical test for hiring

9. Limiting the number of rows from a query!

10. limiting number of rows - php

11. Limit the Number Of Rows In A Stored Procedure

12. Limiting the Number of Rows Returned

13. How limit row number