varchar for loops possible?

From: "J(dot)V(dot)" <jvsrvcs(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: varchar for loops possible?
Date: 2012-05-18 20:30:44
Message-ID: 4FB6B174.7090206@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a table with a varchar column.

I want to select the distinct values from this column and loop through
them (using as a variable) in a raise notice statement and also in an
update statement.

I have not been able to do this trying over 100 things in the last two
hours. I could not find an example on google.

for tmp_var in select distinct(value) from mytable where
value2='literal'
loop
raise notice 'I want to print a message here - the tmp_var is
[' || tmp_var || ']';
update table set varcharid = ''' || tmp_var || '''
end loop;

tmp_var has to be in ' ' ticks or will not work. it is failing on the
first FOR statment stating: "invalid input syntax for integer:
"some_distinct_value".

How do I select varchar distinct values and iterate using variables in a
raise notice statement and inside another update statement?

this seems simple to do , but have not found a way.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-05-18 20:40:07 Re: varchar for loops possible?
Previous Message David Johnston 2012-05-18 18:30:22 Re: Fetching multiple rows in single round trip