Re: Problem with FOUND - Mailing list pgsql-general

From Tom Lane
Subject Re: Problem with FOUND
Date
Msg-id 16346.1214493204@sss.pgh.pa.us
Whole thread Raw
In response to Problem with FOUND  ("A B" <gentosaker@gmail.com>)
Responses Re: Problem with FOUND  ("A B" <gentosaker@gmail.com>)
List pgsql-general
"A B" <gentosaker@gmail.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

pgsql-general by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Unicode problem again
Next
From: Garry Saddington
Date:
Subject: Re: Unicode problem again