postgres中的函数返回许多行,其中包含一些空值

时间:2017-07-04 08:47:31

标签: postgresql plpgsql stored-functions

我在postgress手段中的功能几乎没有问题。 我有功能:

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;

当我执行此功能时,我会收到错误:

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

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

如何更正运行此查询并执行此功能???

1 个答案:

答案 0 :(得分:1)

您应该显式指定null的数据类型,例如:

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;