I have this function and is given me the error below the code and want to know which is my mistake
create function pt
(@.idpt INT,
@.option INT)
Returns Real
AS
Begin
if (@.option=1)
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from electron as a
where @.idpt = a.idap)
end
else if(@.option=2)
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from muon as a
where @.idpt = a.idap)
End
else
Begin
Return ( select sqrt(a.px*a.px + a.py*a.py)
from jet as a
where @.idpt = a.idap)
end
end
Msg 455, Level 16, State 2, Procedure pt, Line 22
The last statement included within a function must be a return statement.
Try putting a dummy "RETURN NULL" after the large IF block. I think the compiler isn't realizing that at least one of the 3 return statements you've got will always be called, and it's throwing a fit. The extra RETURN will never get called, but it'll at least keep the compiler happy.
|||
Try the code below.
Chris
Code Snippet
CREATE FUNCTION pt (@.idpt INT, @.option INT)
RETURNS REAL
AS
BEGIN
DECLARE @.Output REAL
IF (@.option = 1)
BEGIN
SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)
FROM electron AS a
WHERE @.idpt = a.idap
END
ELSE
IF (@.option = 2)
BEGIN
SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)
FROM muon AS a
WHERE @.idpt = a.idap
END
ELSE
BEGIN
SELECT @.Output = SQRT(a.px * a.px + a.py * a.py)
FROM jet AS a
WHERE @.idpt = a.idap
END
RETURN @.Output
END
No comments:
Post a Comment