i am trying to create a sql function that prints the reservations made by a customer when his name is beeing given. The customers names are in a table called CLIENTS.FNAME CLIENTS.MNAME CLIENTS.LNAME and the reservations searched by an id- foreign key there is the code for the function that shows a 'too many values" error, maybe it has to be done by Loop or something?
create or replace FUNCTION cl_reserv(clName VARCHAR2)
RETURN RESERVATIONS%ROWTYPE
IS
resRecord RESERVATIONS%ROWTYPE;
BEGIN
SELECT RESID,STARTDATE,ENDDATE,ADDINFO INTO resRecord
FROM RESERVATIONS INNER JOIN CLIENTS ON RESERVATIONS.CLIENTID=CLIENTS.CLIENTID
WHERE clName IN (FNAME,MNAME,LNAME);
RETURN resRecord;
END;
Your SELECT clause doesn't match with the record you're trying to select into. Change your code like this:
CREATE OR REPLACE FUNCTION cl_reserv(clName VARCHAR2)
RETURN RESERVATIONS%ROWTYPE
IS
resRecord RESERVATIONS%ROWTYPE;
BEGIN
SELECT r.* INTO resRecord
FROM RESERVATIONS r INNER JOIN CLIENTS c ON r.CLIENTID=c.CLIENTID
WHERE clName IN (FNAME,MNAME,LNAME);
RETURN resRecord;
END cl_reserv;
By using r.*
, you select exactly the number, type and sequence of columns that are needed for the record type.