Re: Setting up functions in psql.

Поиск
Список
Период
Сортировка
От Clodoaldo
Тема Re: Setting up functions in psql.
Дата
Msg-id a595de7a0702160413l623d12c4hbfd0dc3ea6db5f6c@mail.gmail.com
обсуждение исходный текст
Ответ на Setting up functions in psql.  (Paul Lambert <paul.lambert@autoledgers.com.au>)
Список pgsql-general
2007/2/16, Paul Lambert <paul.lambert@autoledgers.com.au>:
> In setting up some functions to load data from a csv file, I'm doing the
> following in psql on Weendoze:
>
> AutoDRS=# CREATE OR REPLACE FUNCTION "fnLoadAppraisals"()
> AutoDRS-#   RETURNS void AS
> AutoDRS-# $BODY$
> AutoDRS$#       DROP TABLE IF EXISTS appraisals_temp_load;
> AutoDRS$#       CREATE TABLE appraisals_temp_load AS SELECT * FROM
> appraisals WHERE 1=0;
> AutoDRS$#       TRUNCATE TABLE appraisals;
> AutoDRS$#       COPY appraisals_temp_load FROM
> 'c:/temp/autodrs_appraisal.txt' WITH DELIMITER AS '^' CSV HEADER;
> AutoDRS$#       INSERT INTO appraisals (SELECT DISTINCT ON
> (dealer_id,appraisal_id) * FROM appraisals_temp_load);
> AutoDRS$#       DROP TABLE IF EXISTS appraisals_temp_load;
> AutoDRS$# $BODY$
> AutoDRS-#   LANGUAGE 'sql' VOLATILE;
> ERROR:  relation "appraisals_temp_load" does not exist
> CONTEXT:  SQL function "fnLoadAppraisals"
> AutoDRS=# ALTER FUNCTION "fnLoadAppraisals"() OWNER TO "AutoDRS";
> ERROR:  function fnLoadAppraisals() does not exist
>
> I can see why the error occurs, the table "appraisals_temp_load" is
> being created and then deleted - I don't leave it in the database.
>
> What I am confused about is: Why does the creation of a function fail if
> a table it uses does not exist when the function itself is creating the
> table further up to where it references it?
>
> Should I be doing this in a different way?
>
> (Yes I know it's easy enough to just create the table before creating
> the function, I'm just curious as to why it should fail)

In instead of droping and creating the table at each function
execution you could create the table only once out of the function and
then truncate it inside the function.

Regards,
--
Clodoaldo Pinto Neto

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

Предыдущее
От: Bill Moran
Дата:
Сообщение: Re: how to hide database objects from users, that don't have access to them
Следующее
От: Tomas Vondra
Дата:
Сообщение: Re: Setting up functions in psql.