CREATE TYPE EnumerationDetails AS(EnumOrdinal BIGINT, EnumText VARCHAR(255));

CREATE OR REPLACE FUNCTION PP_ReadEnum(varchar,varchar) RETURNS SETOF EnumerationDetails AS '
DECLARE
	szPointName ALIAS FOR $1;
	szAttributeName ALIAS FOR $2;
	nPointId BIGINT;
	nAttributeId BIGINT;
	IsFixFlag BIT;
	IsExpFlag BIT;
	Rec EnumerationDetails;
BEGIN
	IF szPointName IS NULL OR szAttributeName IS NULL THEN
		RAISE EXCEPTION ''PP_ReadEnum(): Parameters passed cannot be NULL'';

	SELECT INTO nPointId PointId
	FROM PointTable
	WHERE PointName = szPointName;

	SELECT INTO nAttributeId,IsFixFlag,IsExpFlag AttributeId,IsFixEnum,IsExpandEnum
	FROM Attributes
	WHERE AttributeName = szAttributeName;
	RAISE NOTICE ''PointId=%, AttributeId=%, FixEnum=%, ExpEnum=%'',nPointId,nAttributeId,IsFixFlag,IsExpFlag;

	IF IsFixFlag = ''1'' THEN
		FOR Rec IN SELECT FixEnumOrdinal,FixEnumText
		FROM FixEnumeration
		WHERE AttributeId = nAttributeId LOOP
		RETURN NEXT REC;
		END LOOP;
		RETURN;
	ELSIF IsExpFlag = ''1'' THEN
		FOR Rec IN SELECT ExpEnumOrdinal,ExpEnumText
		FROM ExpandEnumeration
		WHERE AttributeId = nAttributeId AND PointId = nPointId LOOP
		RETURN NEXT REC;
		END LOOP;
		RETURN;
	ELSE  -- Line 41
		RAISE EXCEPTION ''The Point Attribute does not have Enum Data.'';
	END IF;

END;
' LANGUAGE 'plpgsql';
		