Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a single row with t-sql

Tags:

I want to update a row in my date base. The problem is, through a mistake on my part, I have two identical rows of data. How do I run the update on just one row?

like image 491
dan_vitch Avatar asked May 25 '10 22:05

dan_vitch


People also ask

How do you update an entire row in SQL?

To update an entire row in MySQL, use UPDATE command. You need to know the primary key column. The syntax is as follows to update an entire row. The following is the query to update an entire row in MySQL.


2 Answers

In SQL Server 2005+, you can use

UPDATE TOP (1) ....

The advantage of this over SET ROWCOUNT is that any triggers will not be subject to a ROWCOUNT limit, which is almost certainly a good thing.

like image 70
spender Avatar answered Sep 18 '22 01:09

spender


Often tables have a unique ID. And you should filter on that.

For example,

UPDATE YourTable
SET YourColumnToUpdate = 'your_value'
WHERE YourUniqueColumn = @Id

If your table does not have a unique ID, consider adding one: an integer column with a Primary Key and Identity.

like image 39
Bill Paetzke Avatar answered Sep 19 '22 01:09

Bill Paetzke