4

I have a table that look like this:

property   propertyid  value
 active     1           1
 datastore  2           apc

How would I formulate a SQL query to update a few rows, without updating all of them. IE, if there were 6 rows, update 2,3 and 4 but not 1,5 and 6?

Thanks.

4
  • Update "both" of what? What column is 1-6? Commented Nov 28, 2010 at 18:08
  • Clarification: The table has three columns: property, propertyid and value. Clarified both in question. Commented Nov 28, 2010 at 18:10
  • 1
    update different rows with same value or different values? Commented Nov 28, 2010 at 18:17
  • What identifies 2 & 3 for updating? Commented Nov 28, 2010 at 18:25

1 Answer 1

9

If it is the same column(s) that you are updating on all the rows with the same value you can do it easily with a query like this.

UPDATE property SET value=5 where propertyid IN(2,3,4)

This will set the value 5 to all rows where the property id is either 2,3 or 4.

If you want to update different rows with different values, I am afraid you will have to write separate SQL statements. You could come with a SQL query using the CASE statement but it would be much readable and maintainable with a simple SQL query.

Assuming that you are manipulating the database from an application, I am pretty sure that with whatever programming language you will be using, it will be easy as writing 1 SQL statement and looping, replacing the values and executing the query OR appending all the SQL update statements to a string (while looping) and pass it to the database to execute all at once. I am sorry I don't know whether either would have a significant performance impact over the other, but I believe that executing all at once would have some benefit in regards to performance.

Sign up to request clarification or add additional context in comments.

1 Comment

Whilst this wasn't the exact answer I was looking for, you did confirm my suspicion that what I wanted to achieve wasn't achievable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.