90

I have a table table1 with three columns and a bunch of rows:

[key_col|col_a|col_b] 

I want to update col_a with a set of values (i.e. leaving col_b unchanged), something like this:

INSERT INTO table1 AS t1 (key_col, col_a) VALUES ("k1", "foo"), ("k2", "bar"); 


But it doesn't work, how do I do this?

2

5 Answers 5

134

You have to use UPDATE instead of INSERT:

For Example:

UPDATE table1 SET col_a='k1', col_b='foo' WHERE key_col='1'; UPDATE table1 SET col_a='k2', col_b='bar' WHERE key_col='2'; 
Sign up to request clarification or add additional context in comments.

4 Comments

so you are saying that i need to write one update query for each row I want to update?
Read this article for multiple rows update: karlrixon.co.uk/articles/sql/…
The link Update Multiple Rows With Different Values and a Single SQL Query is broken. Fix or remove please.
You can use a batch from your client application .
12
UPDATE table1 SET col_a = 'newvalue' 

Add a WHERE condition if you want to only update some of the rows.

2 Comments

could you give an example where multiply rows are updated in same query
This would update all rows. Use a where to limit this to less rows.
7

This is what I did for bulk update:

UPDATE tableName SET isDeleted = 1 where columnName in ('430903GW4j683537882','430903GW4j667075431','430903GW4j658444015') 

Comments

3

if you want to fill all the column:

update 'column' set 'info' where keyID!=0; 

Comments

1

If you want to update data you should use UPDATE command instead of INSERT

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.