我正在尝试编写一个 PL/SQL 函数,该函数将球员的姓名作为参数,并返回在他最喜欢的体育场进行的比赛数。
如果玩家表中不存在玩家,该函数应返回 -2。如果玩家存在但他没有最喜欢的体育场,则该函数返回 -1。
这就是我所拥有的:
create or replace function favS(pname varchar2) return number
as
fav_stadium_count number;
begin
select count(case when favstadiums.stadium = matches.stadium then 1 else null end) into fav_stadium_count
from favstadiums
right join players
on favstadiums.player = players.name
outer join matches
on favstadiums.stadium = matches.stadium;
if (count(case when favstadiums.stadium = matches.stadium then 1 else null end) > 0) then
return fav_stadium_count;
end if;
if players.name is null then
return -2;
end if;
if (count(case when favstadiums.stadium = matches.stadium then 1 else null end) < 1) then
return -1;
end if;
end;
但我得到以下编译错误:
Line 9: ORA-00933: SQL command not properly ended
Line 5: SQL Statement ignored
有想法该怎么解决这个吗?
如果有帮助,这是数据库的关系图:
编辑(特德):
create or replace function favS(pname varchar2) return number
as
fav_stadium_count number;
vplayername varchar(100);
begin
select count(case when favstadiums.stadium = matches.stadium then 1 else null end) into fav_stadium_count,
players.name into vplayername
from favstadiums
right join players
on favstadiums.player = players.name
left outer join matches
on favstadiums.stadium = matches.stadium
where name = pname;
if (fav_stadium_count > 0) then
return fav_stadium_count;
end if;
if vplayername is null then
return -2;
end if;
if (fav_stadium_count < 1) then
return -1;
end if;
end;