Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 2082B632CBB for ; Thu, 22 Oct 2009 08:36:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 35799-05 for ; Thu, 22 Oct 2009 11:36:22 +0000 (UTC) X-Greylist: delayed 00:20:32.243791 by SQLgrey-1.7.6 Received: from mail.rp-online.de (mail.rp-online.de [149.221.1.199]) by mail.postgresql.org (Postfix) with ESMTP id E4F466321BE for ; Thu, 22 Oct 2009 08:36:27 -0300 (ADT) Received: from enternix.d.rp-online.de (enternix.d.rp-online.de [149.221.100.191]) by mail.rp-online.de (Postfix) with ESMTP id E7D38380B; Thu, 22 Oct 2009 13:15:53 +0200 (CEST) Message-ID: <4AE03EED.105@rp-online.de> Date: Thu, 22 Oct 2009 13:15:57 +0200 From: Thomas Pundt Reply-To: thomas.pundt@rp-online.de Organization: RP Online GmbH User-Agent: Thunderbird 2.0.0.23 (Macintosh/20090812) MIME-Version: 1.0 To: maboyz CC: pgsql-sql@postgresql.org Subject: Re: @@Error equivalent in Postgresql References: <25995788.post@talk.nabble.com> In-Reply-To: <25995788.post@talk.nabble.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.10/RELEASE, bases: 22102009 #2815223, status: clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/94 X-Sequence-Number: 33293 Hi maboyz schrieb: > I am in the process of migrating our database from MS Server 2000 to > Postgres. I have a bunch of stored procs which i have to modify the syntax > so they work in postgresql. My ? is is there an equivalent for the @@Error > function in T-SQL for postgres: The stored proc i am converting is: > > ALTER PROCEDURE [dbo].[AuditAccounts] > > @ReturnValue int output > AS > > SET NOCOUNT ON > > select * from > AdminAccts full join AmAccts > on adm_acc_AccountNo = am_acc_AccountNo > where > adm_acc_AccountNo is null > or am_acc_AccountNo is null > > Set @ReturnValue = @@Error > > I have wriiten the postgres function as follows : > > CREATE TYPE AuditAccount AS (adm_acc_AccountNo character varying, > am_acc_AccountNo character varying); > CREATE FUNCTION dint_AuditAccounts( ) > RETURNS SETOF AuditAccount AS > $BODY$ > BEGIN > RETURN QUERY > select * from "AdminAccounts" > full join "AmAccounts" > on "adm_acc_AccountNo" = "am_acc_AccountNo" > where "adm_acc_AccountNo" is null or "am_acc_AccountNo" is null; > > END; > $BODY$ > LANGUAGE 'plpgsql' VOLATILE > COST 100 > ROWS 10; > > How do i implement exception handling in this case, if i want the function > to report back successful execution or failure just like the @@Error > function does in T-SQL? I have no clue about T-SQL, but I think you can easily extend your function(s) to use PL/pgSQL exception handling described here: http://www.postgresql.org/docs/8.4/interactive/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING BEGIN statements EXCEPTION WHEN condition [ OR condition ... ] THEN handler_statements [ WHEN condition [ OR condition ... ] THEN handler_statements ... ] END; Ciao, Thomas