Capturing return value of a function

Started by Jasbinder Balialmost 19 years ago4 messagesgeneral
Jump to latest
#1Jasbinder Bali
jsbali@gmail.com

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;

#2Michael Glaesemann
grzm@seespotcode.net
In reply to: Jasbinder Bali (#1)
Re: Capturing return value of a function

On Jul 16, 2007, at 15:55 , Jasbinder Singh Bali wrote:

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.

Depends on what you mean by "failed". Do you mean an error was
raised? Then you'll just get a normal error message. To catch this,
check the error trapping section of the PL/pgSQL documentation.

Do you mean nothing was inserted? If so, you won't catch this in your
above code. You can check the FOUND special variable after the INSERT
to see if anything was inserted and set your result message based on
that.

Would the following implementation work for that?

No. BEGIN/END blocks in PL/pgSQL are not equivalent to SQL BEGIN/
COMMIT transactions.

Michael Glaesemann
grzm seespotcode net

#3Bruce Momjian
bruce@momjian.us
In reply to: Jasbinder Bali (#1)
Re: Capturing return value of a function

"Jasbinder Singh Bali" <jsbali@gmail.com> writes:

I was wondering what would the function return if insert fails.

It would abort immediately and abort your transaction as well unless something
higher up catches the error.

I want it to return 'success' upon a successful insert and 'failure' if
insert fails.

You need an EXCEPT WHEN clause, see:

http://www.postgresql.org/docs/8.2/interactive/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

#4George Weaver
gweaver@shaw.ca
In reply to: Jasbinder Bali (#1)
Re: Capturing return value of a function

----- Original Message -----
From: Jasbinder Singh Bali
To: pgsql-general@postgresql.org
Sent: Monday, July 16, 2007 3:55 PM
Subject: [GENERAL] Capturing return value of a function

<snip>

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.

You can find out whether the insert was successful or not using the tools
here:

http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS

<snip>

Regards,
George