Re: call the same pl/pgsql procedure twice in the same connection

Поиск
Список
Период
Сортировка
От Bruce Momjian
Тема Re: call the same pl/pgsql procedure twice in the same connection
Дата
Msg-id 200203252106.g2PL6dv16423@candle.pha.pa.us
обсуждение исходный текст
Ответ на call the same pl/pgsql procedure twice in the same connection session  ("jack" <datactrl@tpg.com.au>)
Ответы Re: call the same pl/pgsql procedure twice in the same connection  (Jan Wieck <janwieck@yahoo.com>)
Список pgsql-sql
jack wrote:
> I have a function with pl/pgSQL such as...
> CREATE FUNCTION comp_userExp (TEXT) RETURNS INTEGER  AS '
> DECLARE
>  stUserName TEXT;
> BEGIN
>  stUserName := upper($1);
> 
>  CREATE TEMP TABLE comuser AS
>   SELECT * FROM comt_user1
>   WHERE userName=stUserName;
> 
>  CREATE TEMP TABLE comUser1 AS
>   SELECT a.userName FROM comt_user2 a, comuser b
>   WHERE a.userName = b.userName
>   ORDER BY b.userName;
> 
>  CREATE TEMP TABLE comUser2 AS
>   SELECT a.userName FROM comt_user3 a, comuser b
>   WHERE a.userName = b.userName
>   ORDER BY b.userName;
> 
>  DROP Table comuser,comuser1,comuser2;
> 
>  RETURN 0;
> 
> END;'
>   LANGUAGE 'PLPGSQL';
> 
> This function can't run twice in the same connection session. After tracing
> error, the reason is because 2nd and 3rd SQL refer to table comuser. It will
> cause "can't find relation number xxxxx". I think ,after first run, the
> procedure just use relation number to access table, while actually table was
> dropped and create again with a different relation number on the 2nd time.
> If I disconnect database, and re-connect again, it won't cuase any problem.
> If don't want to disconnect and connect, is there any way to fix the
> problem?

Yes, this is coming up a lot recently, maybe an FAQ.  You need to use
EXECUTE in plpgsql so the string is reparsed every time and the proper
oid assigned.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026
 


В списке pgsql-sql по дате отправления:

Предыдущее
От: Bruce Momjian
Дата:
Сообщение: Re: About temporary table
Следующее
От: Thomas Swan
Дата:
Сообщение: Re: Cleaning up template 1