1

i have little problem with function in postgress means. I have function:

CREATE OR REPLACE FUNCTION test(
    x integer,
    y character varying,
    z character varying
)
 RETURNS TABLE(x1 int, y1 varchar,n1 varchar,z1 varchar) AS
$BODY$
BEGIN
   RETURN QUERY
SELECT  x,y,null,z  FROM example_tab            
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;

When I execute this function I take error:

 Error: Returned type unknown does not match expected type character varying in column 3.

 SELECT * FROM test(694531020,'t'::varchar,'t'::varchar) 

What should I do to correct run this query and execute this function ???

1
  • 1
    SELECT x,y,null::varchar,z FROM example_tab and please remove mysql and sql-server from tags Commented Jul 4, 2017 at 8:58

1 Answer 1

3

You should explicitly specify datatype for null, like:

CREATE OR REPLACE FUNCTION test(
    x integer,
    y character varying,
    z character varying
)
 RETURNS TABLE(x1 int, y1 varchar,n1 varchar,z1 varchar) AS
$BODY$
BEGIN
   RETURN QUERY
SELECT  x,y,null::varchar,z  FROM example_tab            
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
  ROWS 1000;
Sign up to request clarification or add additional context in comments.

2 Comments

What is the logic in casting null (an unknown value) as an integer?
to match types of return table I suppose

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.