SQL - Update multiple records in one query

0 votes

I have table - config. Schema: config_name | config_value . Additionally, I want to edit numerous records with a single query. I attempt to:

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

But it doesn't work. Can someone please help me with this?

Sep 16 in Database by Kithuzzz
• 11,640 points
11 views

1 answer to this question.

0 votes

Try either multi-table update syntax:

UPDATE config t1 JOIN config t2
    ON t1.config_name = 'name1' AND t2.config_name = 'name2'
   SET t1.config_value = 'value',
       t2.config_value = 'value2';

Here is a SQLFiddle demo.

Or conditional update:

UPDATE config
   SET config_value = CASE config_name 
                      WHEN 'name1' THEN 'value' 
                      WHEN 'name2' THEN 'value2' 
                      ELSE config_value
                      END
 WHERE config_name IN('name1', 'name2');

Here is a SQLFiddle demo.

answered Sep 17 by narikkadan
• 19,680 points

Related Questions In Database

0 votes
1 answer

Inserting multiple rows in a single SQL query?

In SQL Server 2008, multiple rows can ...READ MORE

answered Feb 10 in Database by Vaani
• 7,020 points
82 views
0 votes
0 answers

How to update two tables in one statement in SQL Server 2005?

In one operation, I want to update ...READ MORE

Aug 20 in Database by Kithuzzz
• 11,640 points
26 views
0 votes
0 answers

SQL: Two select statements in one query

I want to return the newly created ...READ MORE

Aug 20 in Database by Kithuzzz
• 11,640 points
50 views
0 votes
0 answers

I want to use CASE statement to update some records in sql server 2005

UPDATE dbo.TestStudents SET LASTNAME = ( ...READ MORE

Sep 2 in Database by Kithuzzz
• 11,640 points
32 views
0 votes
1 answer

Want a command to be executed in Salt only if a directory is empty

You should consider using this inside your ...READ MORE

answered Jun 12, 2018 in DevOps Tools by Damon Salvatore
• 5,980 points
755 views
0 votes
1 answer

Creating A New MySQL User In Amazon RDS Environment

AWS RDS security groups documentation (a common ...READ MORE

answered Jul 18, 2018 in AWS by Priyaj
• 58,080 points
1,396 views
0 votes
1 answer

Using conditional to execute a command in salt

You can use unless inside your state ...READ MORE

answered Jul 23, 2018 in Other DevOps Questions by DareDev
• 6,890 points
820 views
0 votes
1 answer

Multiple rows to one comma-separated value in Sql Server

Test Data DECLARE @Table1 TABLE(ID INT, Value INT) INSERT ...READ MORE

answered Sep 17 in Database by narikkadan
• 19,680 points
19 views
0 votes
1 answer

How to delete multiple rows in SQL where id = (x to y)

If you need to delete based on ...READ MORE

answered Sep 17 in Database by narikkadan
• 19,680 points
32 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP