Re: Problem with FOUND

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "A B" <gentosaker(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with FOUND
Date: 2008-06-26 15:13:24
Message-ID: 16346.1214493204@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"A B" <gentosaker(at)gmail(dot)com> writes:
> CREATE OR REPLACE FUNCTION addRating(tbl_ INTEGER,value_ INTEGER)
> RETURNS void AS $$
> DECLARE
> tablename TEXT;
> fieldname TEXT;
> BEGIN
> tablename:='Rating_'||tbl_;
> fieldname:='val';
> EXECUTE 'UPDATE '||tablename||' SET '||fieldname||'='||value_||'
> WHERE '||fieldname||'='||value_ ;
> IF NOT FOUND THEN
> EXECUTE 'INSERT INTO '||tablename||' ('||fieldname||') VALUES ('||value_||')';
> END IF;
> END;
> $$ LANGUAGE plpgsql;

> Can anyone help me spot the error?

EXECUTE doesn't set FOUND.

I think you'd be well advised to rethink your table layout so you don't
need so much dynamic SQL. The above is going to suck on both
performance and readability grounds, and it doesn't look like it's
accomplishing anything you couldn't do by combining all the Rating
tables into one table with an extra key column.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Garry Saddington 2008-06-26 15:27:06 Re: Unicode problem again
Previous Message Michael Fuhr 2008-06-26 14:41:07 Re: Unicode problem again