1

I get a problem to create stored procedure in postgresql with multiple select, this is error that I get

ERROR: function result type must be specified SQL state: 42P13

and this is my query:

create or replace function user_info(
    functn integer
)
language sql 
as 
$$
begin
    
    if(functn = 1) then
        select email, user_name, address from user;
        
    else if(functn = 2) then
        select email, member_name, phone from member;
        
    else
        select email, admin_name, post_code from admin;
    
    end if;
    
end
$$;

please anyone help me to solve this problem, thanks.

1
  • You're doing CREATE FUNCTION without a RETURNS clause. (And also, your function doesn't do anything and returns nothing) Commented Dec 27, 2021 at 10:16

1 Answer 1

2

There are quite a few syntax errors in your function:

  • You didn't specify the return type.
  • Your function doesn't return anything.
  • Correct syntax is elsif instead of else if.

Try this:

create or replace function user_info(
    functn integer
)
returns table(email text, name text, contact text)
language plpgsql 
as 
$$
begin
    if functn = 1 then
        return query select email, user_name, address from user;
        
    elsif functn = 2 then
        return query select email, member_name, phone from member;
        
    else
        return query select email, admin_name, post_code from admin;
    
    end if;

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

Comments

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.