Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Statement with multiple SETs and WHEREs

Tags:

sql

db2

I am wondering if this is a valid query:

UPDATE  table SET ID = 111111259  WHERE ID = 2555  AND SET ID = 111111261  WHERE ID = 2724  AND SET ID = 111111263  WHERE ID = 2021  AND SET ID = 111111264  WHERE ID = 2017 
like image 547
ProgramNov Avatar asked Jun 22 '11 20:06

ProgramNov


People also ask

Can you have multiple WHERE statements in SQL?

You can use the OR condition in the WHERE clause to test multiple conditions where the record is returned if any one of the conditions are met.

Can you do multiple sets in SQL update?

Can we UPDATE multiple tables with a single SQL query? No, only 1 table can be updated with an UPDATE statement. However, you can use a transaction to ensure that 2 or more UPDATE statements are processed as a single unit-of-work.

Can WHERE clause have multiple values?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

Can we use and in WHERE clause in SQL?

SQL AND, OR and NOT OperatorsThe WHERE clause can be combined with AND , OR , and NOT operators. The AND and OR operators are used to filter records based on more than one condition: The AND operator displays a record if all the conditions separated by AND are TRUE.


2 Answers

NO!

You'll need to handle those individually

Update [table] Set ID = 111111259 WHERE ID = 2555  Update [table] Set ID = 111111261 WHERE ID = 2724  --... 
like image 80
Brandon Boone Avatar answered Oct 23 '22 13:10

Brandon Boone


Best option is multiple updates.

Alternatively you can do the following but is NOT recommended:

UPDATE table SET ID = CASE WHEN ID = 2555 THEN 111111259                WHEN ID = 2724 THEN 111111261               WHEN ID = 2021 THEN 111111263               WHEN ID = 2017 THEN 111111264          END WHERE ID IN (2555,2724,2021,2017) 
like image 45
niktrs Avatar answered Oct 23 '22 14:10

niktrs