I am trying to run the following function and I get the error "syntax error at or near "$1" Could someone help me out fix this. Thanks!
CREATE OR REPLACE FUNCTION vin_calc() RETURNS text AS
$BODY$
DECLARE
    r res%rowtype;
    i integer default 0;
    x  text;
    curs2 CURSOR FOR SELECT * FROM res;
BEGIN
    FOR r IN curs2 
    LOOP
    if(x!=r.prod_grp_nm)
    i=0 
    end
     i:=i+1;
     if (i=1)
      update res set duration =0 where CURRENT OF curs2;
     elsif(i=2)
     update res set duration =1 where CURRENT OF curs2;
     elsif(i>=3)
     update res set gwma_duration =0.75*lag(res.duration,1)+(0.125*r.dur) where CURRENT OF curs2;
     end    
    x:=r.prod_grp_nm;      
    END LOOP;
    RETURN 'yes';
END
$BODY$
LANGUAGE 'plpgsql' ;
 
    