将多个变量初始化为postgresql存储过程中的记录

时间:2015-08-19 05:51:34

标签: postgresql stored-procedures plpgsql

我有一个存储过程,我将三个变量声明为记录,然后按如下所示进行初始化:

CREATE OR REPLACE FUNCTION calculateUnitPrice() 
RETURNS VOID AS
$$
DECLARE
      amenities       RECORD;
      paramValues RECORD;
      logsumAccessebility RECORD;
      propertyType   integer;
      unitPrice      float;
      freehold       integer;
      tazId          bigint;
      unit            RECORD;
BEGIN
     FOR unit IN (SELECT * from main2012.fm_unit_res)
LOOP
    amenities := getAmenitiesById(unit.sla_address_id);
    tazId := toBigint(amenities.taz_id);
    logsumAccessebility := getLogsumByTazId(tazId);
    propertyType := getPropertyTypeFromUnitType(unit.unit_type);
    paramValues := getParamValuesByPropertyType(propertyType);
    freehold := 0;
    unitPrice := paramValues.intercept + (paramValues.floor_area * ln(unit.floor_area)) + (paramValues.freehold * freehold) + (paramValues.logsum_accessebility * logsumAccessebility.accessibility);
    UPDATE main2012.fm_unit_res SET rent = unitPrice WHERE fm_unit_id = unit.fm_unit_id;
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql; 

但是当我运行该函数时,我收到的错误是这样的:

    ERROR: record "paramvalues" is not assigned yet
    SQL state: 55000
    Detail: The tuple structure of a not-yet-assigned record is indeterminate.

Context: PL/pgSQL function calculateunitprice() line 20 at assignment

请告诉我你的想法。我在这里做错了什么(语法)还是我可以在存储过程中初始化的记录数量有限制?

1 个答案:

答案 0 :(得分:1)

通过添加

来调试输出

raise info '%',getPropertyTypeFromUnitType(unit.unit_type); raise info '%',getPropertyTypeFromUnitType(unit.unit_type); 在声明paramValues

之前