Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update statement to update multiple rows

I have a question regarding the following syntax. Is there a cleaner way to roll this up into one statement rather than two. I've tried several iterations but this seems to be the only way I can successfully execute these two statements.

UPDATE employee SET hire_date = '1979-03-15' WHERE emp_id = 'PMA42628M'   UPDATE employee SET hire_date = '1988-12-22' where emp_id = 'PSA89086M';  

I tried this as well and I also tried using an AND statement. Neither worked. Basically I am looking for a less newbie way then the method above, if one exists. I spent a long time searching and did not find one.

UPDATE employee SET hire_date = ('1979-03-15', '1988-12-22') WHERE emp_id = ('PMA42628M', 'PSA89086M'); 

Appriciate any advice on this one, and by the way, I am using sql server. Thanks

like image 413
user2454335 Avatar asked Jun 05 '13 05:06

user2454335


People also ask

Can we UPDATE multiple rows in a single SQL statement?

MySQL a-z in TeluguColumn values on multiple rows can be updated in a single UPDATE statement if the condition specified in WHERE clause matches multiple rows. In this case, the SET clause will be applied to all the matched rows.

How do you write an UPDATE query for multiple rows in SQL?

UPDATE config SET t1. config_value = 'value' , t2. config_value = 'value2' WHERE t1. config_name = 'name1' AND t2.

How do you UPDATE multiple rows in a column?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.


2 Answers

Try this one, this will combine multiple selects and returns them as if they come from the DB:

UPDATE e SET hire_date = t.hire_date FROM dbo.employee e JOIN (     SELECT emp_id = 'PMA42628M', hire_date = '1979-03-15'     UNION ALL     SELECT emp_id = 'PSA89086M', hire_date = '1988-12-22' ) t ON t.emp_id = e.emp_id 

If you are using SQL Server 2008 or later version, you could also use a different syntax for the derived table:

UPDATE e SET hire_date = t.hire_date FROM dbo.employee e JOIN (     VALUES         ('PMA42628M', '1979-03-15'),         ('PSA89086M', '1988-12-22') ) t (emp_id, hire_date) ON t.emp_id = e.emp_id 
like image 125
Devart Avatar answered Sep 24 '22 05:09

Devart


I am looking for a less newbie way

Doing two separate update statements is (according to me) "the less newbie way" you could complicate stuff and do something like this.

update employee set hire_date = case emp_id                   when 'PMA42628M' then '1979-03-15'                   when 'PSA89086M' then '1988-12-22'                 end where emp_id in ('PMA42628M', 'PSA89086M') 

but what would that gain you? The entire update would run in one implicit transaction so if you want your two updates to be in a transaction you just use begin transaction .... commit.

like image 31
Mikael Eriksson Avatar answered Sep 23 '22 05:09

Mikael Eriksson