创建存储过程以将更改应用于具有大约30个要更改的参数(列)的数据库的最佳方法是什么?
现在我正在玩存储过程中创建一个巨大的插入查询,列出用户传递的所有参数。这是唯一的方法还是有更好的存储过程方式这么大?
示例:
CREATE OR REPLACE FUNCTION "applyntunesettings"(val1,val2....val30)
RETURNS void AS
BEGIN
INSERT INTO calibrationstable (col1,col2........col20) Values (val1,val2.....val20);
INSERT INTO devicestable (col1,col2,.....col10) values (val21,val22,....val30);
END
答案 0 :(得分:1)
有时,行类型变量很方便。假设表格是“校准”和“设备”,我们可以写:
CREATE OR REPLACE FUNCTION applyntunesettings (
_device_name TEXT
, _some_value TEXT
, _other_value TEXT
-- and so on
) RETURNS void AS $$
DECLARE
_calibration calibrations;
_device devices;
BEGIN
_device.name := _device_name;
INSERT INTO devices SELECT _device.*;
_calibration.some_value := _some_value;
_calibration.other_value := _other_value;
INSERT INTO calibrations SELECT _calibration.*;
END $$ LANGUAGE plpgsql;