0

These are the type definitions provided to me

create type IR as (pattern_number integer, uoc_number integer);

My current progress is:

create or replace function q1(pattern text, uoc_threshold integer)
  returns setof IR
  as $$
  BEGIN
    RETURN QUERY
    select count(code) from temp where code like $1;
    RETURN QUERY 
    select count(code) from temp where code like $1 and uoc > $2;

  END;
$$ language plpgsql;

My output needs to be like this : Query:-

select * 
from q1('ECO%', 6);

pattern_number  |   uoc_number 
80              |         5      

I get an error saying:

ERROR: structure of query does not match function result type
DETAIL: Returned type bigint does not match expected type integer in column 1.
CONTEXT: PL/pgSQL function q1(text,integer) line 3 at RETURN QUERY

How do I fix this?

3
  • Your function returns two rows with a single (bigint) column. You want a single row with a single column that is a record. Commented May 2, 2017 at 10:01
  • do I get it right? you want select count(code) from temp where code like $1 as pattern_number and select count(code) from temp where code like $1 and uoc > $2 as uoc_number ?.. I added answer with code - sic? Commented May 2, 2017 at 10:04
  • @VaoTsun Yes. That is exactly what I want but your answer seems to not resolve the error Commented May 2, 2017 at 10:17

1 Answer 1

1

Is it what you want?..

create or replace function q1(pattern text, uoc_threshold integer)
  returns setof IR
  as $$
  BEGIN
    RETURN QUERY
    select (select count(code) from temp where code like $1)::integer
    ,(
    select count(code) from temp where code like $1 and uoc > $2)::integer;

  END;
$$ language plpgsql;
Sign up to request clarification or add additional context in comments.

1 Comment

You got it right but the error is still there. It is the same exact error.

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.