Capturing return value of a function

Поиск
Список
Период
Сортировка
От Jasbinder Singh Bali
Тема Capturing return value of a function
Дата
Msg-id a47902760707161355t30863b29xcc92e3b384f9b698@mail.gmail.com
обсуждение исходный текст
Ответы Re: Capturing return value of a function  (Michael Glaesemann <grzm@seespotcode.net>)
Re: Capturing return value of a function  (Gregory Stark <stark@enterprisedb.com>)
Список pgsql-general
Hi,

I have a function like this


CREATE OR REPLACE FUNCTION sp_insert(text, text, text, text)
  RETURNS text AS
$BODY$
    BEGIN
        INSERT INTO tbl(a,b,c,d)
        VALUES ($1,$2, $3, $4);

        RETURN 'success';
    END;
       
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

I was wondering what would the function return if insert fails.
I want it to return 'success'  upon a successful insert and 'failure' if insert fails.

Would the following implementation work for that?


CREATE OR REPLACE FUNCTION sp_insert(text, text, text, text)
  RETURNS text AS
$BODY$
    BEGIN
                    BEGIN
                            INSERT INTO tbl(a,b,c,d)
                            VALUES ($1,$2, $3, $4);
                            
                                    RETURN 'success';
                    END;

                      RETURN 'failure';
    END;
       
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;

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

Предыдущее
От: Erik Jones
Дата:
Сообщение: Re: deferred check constraints
Следующее
От: Chris Browne
Дата:
Сообщение: Re: SMTP