Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Arithmetic Overflow Error and I cant find it

Author  Topic 

JayDavis
Starting Member

2 Posts

Posted - 2010-05-10 : 16:31:18
Here is the stored procedure

ALTER PROCEDURE usp_GetSeasonStatsByTeamID
@teamid int,
@season int
AS

declare @playertable TABLE(
playerid int,
playername nvarchar(50))

insert into @playertable(playerid, playername)
select id, playerlastname from dbo.player where teamid=@teamid AND active=1


SELECT
p.playerid,
p.playername,
SUM(o.innings) as 'I',
SUM(o.atbats) as 'AB',
SUM(o.runs) as 'R',
SUM(o.hits) as 'H',
SUM(o.singles) as '1B',
SUM(o.doubles) as '2B',
SUM(o.triples) as '3B',
SUM(o.homeruns) as 'HR',
SUM(o.runsbattedin) as 'RBI',
SUM(o.baseonballs) as 'BB',
SUM(o.strikeouts) as 'K',
SUM(o.sacrificeflys) as 'SF',
SUM(o.sacrificehits) as 'SH',
SUM(o.stolenbases) as 'SB',
SUM(o.errors) as 'E',
cast(
cast(
SUM(o.hits) +
SUM(o.baseonballs) as decimal
)
/
case
when
SUM(o.atbats) +
SUM(o.baseonballs) +
SUM(o.sacrificeflys) +
SUM(o.sacrificehits) = 0 then 1
else
cast(
SUM(o.atbats) +
SUM(o.baseonballs) +
SUM(o.sacrificeflys) +
SUM(o.sacrificehits) as decimal
)
end
as decimal(4,3)
) as 'OBP',

cast(
cast(
SUM(o.singles) +
SUM((o.doubles*2)) +
SUM((o.triples*3)) +
SUM((o.homeruns*4))as decimal
)
/
case
when
SUM(o.atbats) = 0 then 1
else
cast(
SUM(o.atbats) as decimal
)
end
as decimal(4,3)
) as 'SLG',

cast(
cast(
SUM(o.hits) as decimal
)
/
case
when
SUM(o.atbats) = 0 then 1
else
cast(
SUM(o.atbats) as decimal
)
end
as decimal(4,3)
) as 'AVG'

from @playertable as p
left join offensivestats as o on p.playerid=o.playerid
Where o.playerid=p.playerid AND o.season=@season
group by p.playerid, p.playername


I get an overflow error, although it appears that everything calculates out correctly by hand. It appears to be coming from the section where I calculate slugging percentage:

cast(cast(SUM(o.singles) + SUM((o.doubles*2)) + SUM((o.triples*3)) + SUM((o.homeruns*4))as decimal) / case when SUM(o.atbats) = 0 then 1 else cast(SUM(o.atbats) as decimal) end as decimal(4,3)) as 'SLG',


Any ideas?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-05-10 : 18:10:37
My guess is that your math produces a number greater than or equal to 10. Which won't fit into a DECIMAL(4,3) (only 1 digit to the left of the decimal).

IE: SELECT CAST(12.0 AS DECIMAL(4,3))

PS: Casting the value as DECIMAL will turn it, bascially, into an INT unless you specify the precision:
SELECT CAST(12.12 AS DECIMAL)
Go to Top of Page
   

- Advertisement -