0

I have a stored procedure where I declare three variables as records and initialize them later as below:

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; 

But when I run the function I am getting an error like this:

    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

Please give me your ideas. Am I doing anything wrong here (syntax) or is there a limit in the number of records I can initialize within a stored procedure?

4
  • maybe cos you assign hparamValues := instead of paramValues :=? :) Commented Aug 19, 2015 at 5:57
  • Thanks for the reply @VaoTsun. Sorry, it was a typing mistake. It is not the problem. I have corrected it in the question. Commented Aug 19, 2015 at 6:02
  • add raise info '%',getPropertyTypeFromUnitType(unit.unit_type);raise info '%',getPropertyTypeFromUnitType(unit.unit_type); before declaring paramValues, then run function and tell the last value of raised info before error?.. Commented Aug 19, 2015 at 6:21
  • 1
    Thanks @VaoTsun. The last value before error was 0 which explains that no paramValues were selected. I have to rewrite the function to handle this I guess. Thanks again. Commented Aug 19, 2015 at 6:31

1 Answer 1

1

Debug the output, by adding

raise info '%',getPropertyTypeFromUnitType(unit.unit_type); raise info '%',getPropertyTypeFromUnitType(unit.unit_type); before declaring paramValues

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.