Building on these tables:
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)
);
I want to make a function that the user will give the circuitcode
and the function will return the name
and surname
of the drivers that in this specific circuit the raceposition
was better than the grid
.
I write this:
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;
And I have this error:
ERROR: return type mismatch in function declared to return f1_driver
DETAIL: Final statement returns too few columns.
CONTEXT: SQL function "get"
The row type f1_driver
does not match what you actually return. Use RETURNS TABLE
to provide a matching declaration:
CREATE FUNCTION f_get_drivers(varchar)
RETURNS TABLE(name varchar, surname varchar)
LANGUAGE sql 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$;
The declared return type in RETURNS
must match what's actually returned.
was the wrong data type for your input parameter. Use character
text
or varchar
. See:
Use an explicit JOIN
doing the same. That's easier to read and maintain.