pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Arulalan Narayanasamy <arulalan.narayanasamy@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Postgres function with output parameters and resultset
Date: Fri, 20 Jul 2018 14:50:58 -0700
Message-ID: <74bea73a-c007-34bc-664a-46a262297f89@aklaver.com> (raw)
In-Reply-To: <CA+Ux585jj_8q-3LeD3TP6T7wXTCi0CZszRyNcCAuvcQ6hpDgVQ@mail.gmail.com>
References: <CA+Ux585jj_8q-3LeD3TP6T7wXTCi0CZszRyNcCAuvcQ6hpDgVQ@mail.gmail.com>

On 07/20/2018 02:19 PM, Arulalan Narayanasamy wrote:
> Hi,
> I need to create a function which should return resultset and output 
> parameters. For example, I need to retrieve all the records from EMP 
> table whose Grade is 'A' as resultset and total number of matched 
> records, Success or Failure flag & Error message as output parameters. 
> Is there a way in Postgres to achieve this? Kindly help!!

Just thinking out loud. Wonder if you could use the multiple cursor 
example shown just above here:

https://www.postgresql.org/docs/10/static/plpgsql-cursors.html#PLPGSQL-CURSOR-FOR-LOOP

Use the one cursor for the result set(NOTE: I did not see where this was 
restricted to 'A' grade)

This is the part I am not sure of. Still here it goes:

1) Create temp table for the message values.

2) Insert the message values in this table.

3) Open a cursor over the table and return it.



> 
> CREATE OR REPLACE FUNCTION TESTFN
> (IN GRADE CHARACTER(01)
> ,OUT EMP_CNT INT
> ,OUT SUCCESS_FG CHARACTER(01)
> ,OUT SQLCD CHARACTER(05)
> ,OUT ERROR_MSG CHARACTER(10))
> RETURNS RECORD
> AS $$
> DECLARE REFCUR REFCURSOR;
> BEGIN
> IF (GRADE IS NULL OR GRADE = '') THEN
> SUCCESS_FG := 'E';
> SQLCD:= '';
> ERROR_MSG := 'GRADE IS NULL OR BLANK';
> RETURN;
> END IF;
> 
> EMP_CNT := SELECT COUNT(*) FROM EMP WHERE GRADE_CD = GRADE;
> 
> EXCEPTION
> WHEN OTHERS THEN
> SUCCESS_FG := 'E';
> SQLCD:= SQLSTATE;
> ERROR_MSG := 'SP EXECUTION ABORTED 1';
> RETURN;
> 
> OPEN REFCUR FOR
> SELECT NAME, AGE, BRANCH, TITLE FROM EMP WHERE GRADE_CD = GRADE;
> 
> EXCEPTION
> WHEN OTHERS THEN
> SUCCESS_FG := 'E';
> SQLCD:= SQLSTATE;
> ERROR_MSG := 'SP EXECUTION ABORTED 2';
> RETURN;
> 
> RETURN REFCUR;
> END;
> $$ LANGUAGE plpgsql;


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (3+ messages)

Message-ID: <74bea73a-c007-34bc-664a-46a262297f89@aklaver.com>
Permalink:  ../74bea73a-c007-34bc-664a-46a262297f89@aklaver.com/
Also on:    postgresql.org/message-id/74bea73a-c007-34bc-664a-46a262297f89@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, arulalan.narayanasamy@gmail.com
  Subject: Re: Postgres function with output parameters and resultset
  In-Reply-To: <74bea73a-c007-34bc-664a-46a262297f89@aklaver.com>
  References: <CA+Ux585jj_8q-3LeD3TP6T7wXTCi0CZszRyNcCAuvcQ6hpDgVQ@mail.gmail.com> <74bea73a-c007-34bc-664a-46a262297f89@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox