1

I want to create a basic function which takes an input and throws a table with multiple columns and rows as an output . I have written the following query for the same :

create or replace function proc_name_1 (store text)
returns table (question text , resp numeric , gendr text , resp_count integer) 
as $$
begin 
return query 
select question_type , response, gender, sum(response_count) 
from fashtagcompute.response_count
where store_id = store 
group by question_type , response, gender ; 
end ; 
$$ 
language plpgsql ; 
CREATE FUNCTION

The function is succesfully created. However if I try and execute it , I get the following error

select * from proc_name_1('ab1f47ff-5c96-46fb-b975-81bf92c01b63')
ERROR: structure of query does not match function result type 
DETAIL: Returned type character varying(80) does not match expected type text in column 1. 
CONTEXT: PL/pgSQL function proc_name_1(text) line 3 at RETURN QUERY

Please help. Thanks

asked Oct 21, 2018 at 14:41
1
  • The error message, if you read it, tells you exactly what the problem is. So, what's your question? Commented Oct 22, 2018 at 12:46

1 Answer 1

7

It seems that question_type is defined as varchar(80) but you as defined question as text.

Simply cast it as text:

select question_type::text
answered Oct 21, 2018 at 14:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.