为了计算联赛,我做了一个函数。问题是当我尝试查看视图时,该函数给了我下一个错误。 ORA-06503:PL / SQL:函数返回无值 ORA-06512:位于“ project.league”的第24行 06503. 00000-“ PL / SQL:函数返回的值没有值”
这是我的功能代码。
create or replace FUNCTION leaguepts(Team preleague.Team1%TYPE)
RETURN NUMBER
AS
cumulpts NUMBER(3):=1;
cumulpts2 NUMBER(3):=2;
cumulpts3 NUMBER(3):=0;
VT matches.Team1%TYPE;
texist EXCEPTION;
BEGIN
SELECT COUNT(Team1) INTO VT FROM matches WHERE Team1=Team;
IF (VT>0) THEN
SELECT SUM(PTS) INTO cumulpts FROM preleague WHERE Team2=Team;
SELECT SUM(PTS2) INTO cumulpts2 FROM preleague WHERE Team2=Team;
cumulpts3:=(cumulpts+cumulpts2);
ELSE
RAISE texist;
END IF;
RETURN cumulpts3;
EXCEPTION
WHEN texist THEN
DBMS_OUTPUT.PUT_LINE ('The team does not exist');
END;
这是我的查看代码
CREATE OR REPLACE FORCE EDITIONABLE VIEW "project"."league" ("Team", "PTS") AS
SELECT Name, Leaguepts(team_cod) FROM Teams;
我想做一个函数,以了解每支球队必须像透明化一样看到联盟的观点。我将感谢所有尝试帮助我的人。
答案 0 :(得分:1)
@Boneist-好点。
不仅删除了不必要的变量,而且(更重要的是)删除了不必要的查询。上下文在SQL和PL / SQL引擎之间切换或切换会影响性能。
通过删除两个变量和一个SQL查询,我将走得更远:
CREATE OR REPLACE FUNCTION leaguepts (team preleague.team1%TYPE) RETURN NUMBER AS
cumulpts NUMBER(3) := 0;
texist EXCEPTION;
BEGIN
SELECT SUM(NVL(pts, 0) + NVL(pts2, 0))
INTO cumulpts
FROM preleague
WHERE team2 = team;
IF cumulpts > 0 THEN
RETURN cumulpts;
ELSE
RAISE texist;
END IF;
EXCEPTION
WHEN texist THEN
dbms_output.put_line ('The team "'||team||'" does not exist');
return null;
END leaguepts;
/
答案 1 :(得分:0)
我会将您的函数重写为:
CREATE OR REPLACE FUNCTION leaguepts (team preleague.team1%TYPE) RETURN NUMBER AS
cumulpts NUMBER(3) := 0;
cumulpts2 NUMBER(3) := 0;
vt matches.team1%TYPE;
texist EXCEPTION;
BEGIN
SELECT COUNT(team1)
INTO vt
FROM matches
WHERE team1 = team;
IF (vt > 0)
THEN
SELECT SUM(pts), SUM(pts2)
INTO cumulpts, cumulpts2
FROM preleague
WHERE team2 = team;
ELSE
RAISE texist;
END IF;
RETURN nvl(cumulpts, 0) + nvl(cumulpts2, 0);
EXCEPTION
WHEN texist THEN
dbms_output.put_line ('The team "'||team||'" does not exist');
return null;
END leaguepts;
/
我修改了您的函数以删除不必要的cumulpts3
变量,并且还更新了cumulpts + cumulpts2
以使用NVL在任何值均为0的情况下返回0(例如,在我的示例中,球队D存在,但在前级联赛中没有行,因此返回0,而在比赛表中没有球队1值的球队B则返回null。
您可能希望相应地更改这些值(也许您都希望都为空?在这种情况下,请删除NVL
s,或者您要为在场的团队返回null,而对于缺少的团队则返回-1。团队等)