patternsqlMinor
Bulk update of all columns
Viewed 0 times
allbulkcolumnsupdate
Problem
I am wondering if PostgreSQL has an update query somewhat like their insert values syntax.
I have an updated set of data in this form currently:
and I want to update the rows that are there already to the new data, I'm looking for something like the following where I can update all rows without repeating myself:
The above would check for matching
Note: I am really looking to avoid manually mapping all the column names. PostgreSQL already knows my columns, and I've already mapped them on the data. Why do that again in a longer format?
I have an updated set of data in this form currently:
INSERT INTO bought_in_control_panel(ID,PARENT_ID,BOUGHT_IN_FORM_TYPE_ID,PRIORITY,NAME,HEADING,DESCRIPTION,ICON,BOUGHT_IN_CONTROL_PANEL_FILE_ID)
VALUES(109,1,28,100,'Tooling','Tooling','Enter your Machine Tools here','tooling.png',null);and I want to update the rows that are there already to the new data, I'm looking for something like the following where I can update all rows without repeating myself:
UPDATE ON ID
bought_in_control_panel(ID,PARENT_ID,BOUGHT_IN_FORM_TYPE_ID,PRIORITY,NAME,HEADING,DESCRIPTION,ICON,BOUGHT_IN_CONTROL_PANEL_FILE_ID)
VALUES(
(109,1,28,100,'Tooling','Tooling','Enter your Machine Tools here','tooling.png',null),
(1,0,1,200,'Bought In','Bought In','','boughtin.png',null)
);The above would check for matching
ID values, and update the matches with the new data.Note: I am really looking to avoid manually mapping all the column names. PostgreSQL already knows my columns, and I've already mapped them on the data. Why do that again in a longer format?
Solution
If you only want to update data, I'm not sure what the
If you just want to update several rows with a single statement, you might be looking for this:
INSERT statement is for in your question.If you just want to update several rows with a single statement, you might be looking for this:
with update_values (ID,PARENT_ID,BOUGHT_IN_FORM_TYPE_ID,PRIORITY,NAME,HEADING,DESCRIPTION,ICON,BOUGHT_IN_CONTROL_PANEL_FILE_ID) as
(
VALUES
(109,1,28,100,'Tooling','Tooling','Enter your Machine Tools here','tooling.png',null),
(1,0,1,200,'Bought In','Bought In','','boughtin.png',null)
)
update bought_in_control_panel
set parent_id = ud.parent_id,
bought_in_form_type_id = ud.bought_in_form_type_id,
....
from update_values ud
where ud.id = bought_in_control_panel.id;Code Snippets
with update_values (ID,PARENT_ID,BOUGHT_IN_FORM_TYPE_ID,PRIORITY,NAME,HEADING,DESCRIPTION,ICON,BOUGHT_IN_CONTROL_PANEL_FILE_ID) as
(
VALUES
(109,1,28,100,'Tooling','Tooling','Enter your Machine Tools here','tooling.png',null),
(1,0,1,200,'Bought In','Bought In','','boughtin.png',null)
)
update bought_in_control_panel
set parent_id = ud.parent_id,
bought_in_form_type_id = ud.bought_in_form_type_id,
....
from update_values ud
where ud.id = bought_in_control_panel.id;Context
StackExchange Database Administrators Q#45642, answer score: 9
Revisions (0)
No revisions yet.