数据库 批量查询结果作为更新的值
1. 数据库批量更新。
业务需求:
须要将a表中 type为1 的数据的source_id更新为新的id。
由于之前的id是存储在p_server中。如今更新到了server_info表中
update a set source_id = s.server_id from (select p_server.p_id,p_server.index_code,server_info.server_id from p_server join server_info on p_server.index_code = server_info.index_code) s where a.source_id = s.p_id and type = 1;
2. 更新数据库中的值。可是这些值存在不同的表中
CREATE OR REPLACE FUNCTION replace_img_url() RETURNS SETOF refcursor AS $BODY$ DECLARE _ss_urls record; updateVehicle "varchar" := ''; updateImage "varchar" := ''; ss_ip "varchar" := ''; BEGIN for _ss_urls IN EXECUTE 'select svr_id as server_id,svr_ip||'':''||substring(svr_config,E''<httpPort>([0-9]*)?'') as url from server_multi_ip where svr_type = 20002' loop updateImage := 'update alarm_image set image_url = tep.url from (select alarm_event_id as event_id,time_happen,rule_id,sort_num, replace(image_url, '''||_ss_urls.url||''','''||_ss_urls.server_id||''') as url from alarm_image) tep where alarm_image.time_happen = tep.time_happen and alarm_image.alarm_event_id = tep.event_id and alarm_image.rule_id = tep.rule_id and alarm_image.sort_num = tep.sort_num'; execute updateImage; updateVehicle :='update vehicle_recognition set vehicle_image_url = tep.url from (select vehicle_recog_id as id,vehicle_recog_id, replace(vehicle_image_url, '''||_ss_urls.url||''', '''||_ss_urls.server_id||''') as url from vehicle_recognition) tep where vehicle_recognition.vehicle_recog_id = tep.id'; execute updateVehicle; end loop; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000; ALTER FUNCTION replace_img_url() OWNER TO postgres;