0

I am trying to get the data from the Database use function of select prison();.but i got error .Please advise me.

 CREATE OR REPLACE FUNCTION prison() RETURNS refcursor AS $$
 DECLARE
    ref refcursor;
       BEGIN
          OPEN ref FOR SELECT  round,ben_sc,ben_st FROM prison_issue;

          RETURN ref;
       END;
$$ LANGUAGE plpgsql;

and calling like this

     select prison();

also i tried.but cannot executed the rows.

       BEGIN;

      SELECT prison();
      -- Returns: <unnamed portal 2>

      FETCH ALL IN "<unnamed portal 24>";
      COMMIT;
4
  • did not come output...only shows <unnamed portal 24> Commented Sep 23, 2015 at 11:17
  • error means data output shows <unnamed portal 24> Commented Sep 23, 2015 at 11:23
  • 1
    If you want to get data from table you can use "RETURN QUERY" Commented Sep 23, 2015 at 11:25
  • boss..give me any example code Commented Sep 23, 2015 at 11:27

2 Answers 2

1

There is no need for a PL/pgSQL function for this:

CREATE OR REPLACE FUNCTION prison() 
   RETURNS setof prison_issue 
AS $$
  SELECT * FROM prison_issue;
$$ LANGUAGE sql;

You also need to use:

select * from prison();

to retrieve the data, do not use select prison() (which only returns a single record, not multiple rows)

You didn't show us your definition of the table prison_issue if you don't want to return all columns you need something like this:

CREATE OR REPLACE FUNCTION prison() 
   RETURNS table (round integer, ben_sc text, ben_st text)
AS $$
  SELECT SELECT  round,ben_sc,ben_st FROM prison_issue;
$$ LANGUAGE sql;

You will need to adjust the part table (round integer, ben_sc text, ben_st text) to match the data type of the columns you select.

Sign up to request clarification or add additional context in comments.

Comments

0

Below is an example code.I have assumed the data types.Replace with the real ones.

CREATE OR REPLACE FUNCTION prison() RETURNS TABLE(round numeric,ben_sc character varying,ben_st character varying) AS $$

       BEGIN

          RETURN QUERY SELECT  p.round,p.ben_sc,p.ben_st FROM prison_issue p;

       END;
$$ LANGUAGE plpgsql;

8 Comments

I got error...ERROR: column reference "round" is ambiguous LINE 1: SELECT round,ben_sc,ben_st FROM prison_issue ^ DETAIL: It could refer to either a PL/pgSQL variable or a table column. QUERY: SELECT round,ben_sc,ben_st FROM prison_issue CONTEXT: PL/pgSQL function prison_issue() line 5 at RETURN QUERY ********** Error **********
@sarath jay Updated the answer.
CREATE OR REPLACE FUNCTION prison_issue() RETURNS TABLE(round text,ben_sc integer,ben_st integer) AS $$ BEGIN RETURN QUERY SELECT round,ben_sc,ben_st FROM prison_issue; END; $$ LANGUAGE plpgsql;
@sarath jay Use alias "p.". I have updated the answer
boss it's working....the output is (r1,1,1)<br/>(r1,1,2)<br/>(r2,1,3)...but cannot come with seperate column why??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.