错误:声明返回的函数返回类型不匹配

时间:2014-12-27 20:24:22

标签: sql function postgresql types return

以这些表为基础:

create table f1_driver(
 code varchar(5) not null primary key,
 name varchar(10),
 surname varchar(20),
 dateofbirth date,
 debut integer,
 countryoforigin varchar(20),
 points integer
);

create table f1_results (
 drivercode varchar(5) not null references f1_driver,
 circuitcode varchar(5) not null references f1_circuit,
 racedate date,
 raceposition integer,
 grid integer,
 primary key (drivercode, circuitcode,  racedate)
);

我想创建一个用户将提供circuitcode的函数,该函数将返回此特定电路中namesurname的驱动程序raceposition }比grid好。

我写这个:

CREATE FUNCTION get(character) RETURNS SETOF f1_driver AS
$$
SELECT  D.name, D.surname
FROM f1_driver D,f1_results R
WHERE R.circuitcode = $1
AND D.code=R.drivercode
AND R.raceposition<grid ;
$$ LANGUAGE SQL;

我有这个错误:

ERROR:  return type mismatch in function declared to return f1_driver
DETAIL:  Final statement returns too few columns.
CONTEXT:  SQL function "get"

1 个答案:

答案 0 :(得分:0)

行类型f1_driver与您实际返回的内容不匹配。使用RETURNS TABLE提供匹配声明:

CREATE FUNCTION f_get_drivers(varchar)
  RETURNS TABLE(name varchar, surname varchar) AS
$func$
SELECT D.name, D.surname
FROM   f1_driver D
JOIN   f1_results R ON R.drivercode = D.code
WHERE  R.circuitcode = $1
AND    R.raceposition < R.grid;
$func$ LANGUAGE sql;

重点