Skip site navigation (1) Skip section navigation (2)

cursors and for loops?

From: "Dennis" <pg-user(at)calico-consulting(dot)com>
To: "postgres SQL" <pgsql-sql(at)postgresql(dot)org>
Subject: cursors and for loops?
Date: 2004-04-11 04:59:50
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql

I am wondering if I can use a cursor in a for loop. I haven't been able to 
get it to work. I am just beginning plpgsql and I am struggling here. 

I am trying to do this: 

create or replace function ttest(varchar)
RETURNS varchar AS '
	parId	ALIAS FOR $1;
       dennis varchar;
       tmp_xvalues RECORD;

	attrVals		CURSOR (thePar varchar)
		IS select '' '' || name ||''="''|| value ||''"'' as rval
			from attbl where idcol = thePar;

	OPEN attrVals(parId); 

       for tmp_xvalues in fetch all from attrVals loop
           dennis := dennis || tmp_xvalues.rval;
       end loop; 

	return dennis;
' language plpgsql; 

If I try to use this function like so: 

select ttest('blah') 

I get:
Error: ERROR:  syntax error at or near "all" at character 15 

I guess I am confused what I can put in "for var in <expression> loop" and 
what "fetch next" or "fetch all" evaluates to. 



pgsql-sql by date

Next:From: Bret HughesDate: 2004-04-11 05:43:22
Subject: trigger/for key help
Previous:From: Greg StarkDate: 2004-04-11 04:38:20
Subject: Re: SQL challenge--top 10 for each key value?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group