Updating multiple records sql

Rated 3.83/5 based on 646 customer reviews

But in many cases this only provides a modest improvement as each UPDATE operation still requires a round-trip communication with the database server.In the case where the application server and database server are on different hosts, the round-trip will involve network latency as well.A requirement arises in many systems to update multiple SQL database rows.For small numbers of rows requiring updates, it can be adequate to use an UPDATE statement for each row that requires an update.Visit Stack Exchange Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Sign up to join this community CREATE TABLE one ( record_id SERIAL, one_first_var INTEGER, one_second_var INTEGER, one_third_var INTEGER ); INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (1, 1, 1); INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (2, 2, 2); INSERT INTO one (one_first_var, one_second_var, one_third_var) VALUES (3, 3, 3); CREATE TABLE two ( record_id SERIAL, two_first_var INTEGER, two_second_var INTEGER, two_third_var INTEGER ); INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (21, 21, 21); INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (22, 22, 22); INSERT INTO two (two_first_var, two_second_var, two_third_var) VALUES (23, 23, 23); WITH my_values AS ( SELECT one_first_var, one_second_var, one_third_var FROM one WHERE one_first_var = 2 ) UPDATE two SET two_first_var = my_values.one_first_var, two_second_var = my_values.one_second_var, two_third_var = my_values.one_third_var FROM my_values WHERE two_second_var = 22; UPDATE tgt SET Column1 = src. Target Table AS tgt INNER JOIN ( VALUES (1, 'a', 'k', 'x', ...), (2, 'b', 'l', 'y', ...), (3, 'c', 'm', 'z', ...) ) AS src (ID, Column1, Column2, Column3, ...) ON tgt. The dominant factor in the time taken to complete the overall operation tends to be the “admin” work in conveying the application’s intention to the database server rather than the actual updates to the database.A more effective solution to this problem is to attempt to reduce the number of UPDATE statements.

The traditional advice for improving performance for multiple UPDATE statements is to “prepare” the required query once, and then “execute” the prepared query once for each row requiring an update.

Let us start with a simple table: UPDATE staff SET salary = 1200 WHERE name = ' Bob'; UPDATE staff SET salary = 1200 WHERE name = ' Jane'; UPDATE staff SET salary = 1200 WHERE name = ' Frank'; UPDATE staff SET salary = 1200 WHERE name = ' Susan'; UPDATE staff SET salary = 1200 WHERE name = ' John'; UPDATE staff SET salary = 1200 WHERE name = ' Bob'; UPDATE staff SET salary = 1250 WHERE name = ' Jane'; UPDATE staff SET salary = 1200 WHERE name = ' Frank'; UPDATE staff SET salary = 1250 WHERE name = ' Susan'; UPDATE staff SET salary = 1200 WHERE name = ' John'; We are no longer setting all the salary fields to the same value, so we can’t collapse it into a single statement.

But we can group the updates according to the value being set, and then do one UPDATE statement per distinct value.

The solution is everywhere but to me it looks difficult to understand.

For instance, three updates into 1 query: UPDATE table_users SET cod_user = '622057' , date = '12082014' WHERE user_rol = 'student' AND cod_office = '17389551'; UPDATE table_users SET cod_user = '2913659' , date = '12082014' WHERE user_rol = 'assistant' AND cod_office = '17389551'; UPDATE table_users SET cod_user = '6160230' , date = '12082014' WHERE user_rol = 'admin' AND cod_office = '17389551'; UPDATE table_users SET cod_user = (case when user_role = 'student' then '622057' when user_role = 'assistant' then '2913659' when user_role = 'admin' then '6160230' end), date = '12082014' WHERE user_role in ('student', 'assistant', 'admin') AND cod_office = '17389551'; My SQL allows a more readable way to combine multiple updates into a single query.

Leave a Reply