Advertisement

08.04.2008 at 10:08PM PDT, ID: 23621171
[x]
Attachment Details

Arithmetic overflow error converting expression to data type money

Asked by donpick in MS SQL Server

Tags: Microsoft, SQL Server, 2000

Running Small Business Server 2003 premium.

Im having a terrible time trying to determine why I keep getting
Arithmetic overflow error converting expression to data type money.

Question 1.)

I get the error:
Server: Msg 8115, Level 16, State 1, Procedure pCalcTwiggs_250, Line 172
Arithmetic overflow error converting expression to data type money.

Procedure pCalcTwiggs_250 is a compiled stored procedure.   How do you find line 172?  Do you right click on the stored procedure in EM and view properties?  When I do this, it shows me this line:

SET @currLastTwiggsVolumeTEMP       = @currLastTwiggsVolumeTEMP / @currTwiggsDaysDenom

At the bottom right of the window I see 72,  172/246.

Question 2.)  What does the 72 mean?

Question 3.)
  If line 172 is the one above the problem does not occur there.  The error happens only when @currLastTwiggsVolumeTEMP
   is written to a table (see >> here below).
This is so incredibly frustrating.  When I remove @currLastTwiggsVolumeTEMP from the INSERT statement then everything works well, no errors.  When I add it back then the errors occur.  The line containing the INSERT statement is NOT line 172.

How do I troubleshoot this?  I have to get this project done.


Here is the relevant code
== Twiggs Calculation:
--                  == Assign integers to money to allow fractions
            SET @currTwiggsDaysDiff            = @intTwiggsDays - 1
            SET @currTwiggsDaysDenom      = @currTwiggsDaysDiff / @intTwiggsDays
            SET @currAdvDeclineYest            = @currAdvDeclineYest / @currTwiggsDaysDenom
             SET @currLastTwiggsVolumeTEMP       = CAST(@intLastTwiggsVolume AS MONEY)
Line 172>             SET @currLastTwiggsVolumeTEMP       = @currLastTwiggsVolumeTEMP / @currTwiggsDaysDenom
             SET @intLastTwiggsVolume      = CAST(@currLastTwiggsVolumeTEMP AS BIGINT)
--   ==  Cannot use next line because int divided by money produces arithmetic overflow
--            SET @intLastTwiggsVolume      = @intLastTwiggsVolume / @currTwiggsDaysDenom
--            * * * * * * * * * * * * * * * *
            SET @currAdvDecline            = @currAdvDeclineYest + @currAdvDecline
            SET @intCurrentTwiggsVolume      = @intLastTwiggsVolume + @intVolumeToday
            SET @currTwiggsCalc_250            = @currAdvDecline / CAST(@intCurrentTwiggsVolume AS MONEY)
-- orig            SET @currTwiggsCalc_250            = @currAdvDecline / @intCurrentTwiggsVolume


            IF (@@ERROR <> 0)
                BEGIN
                    SET @strComments = 'Label 3100: After Calc Twiggs - division by zero?'
                  SET  @intError = @@ERROR
                  SET  @strComments = 'Error ' +
                                       CAST(@intError AS CHAR(9))
                                       + ' occurred'
                  EXEC ptblErrorLog
                          @strUserID            = @strUserID            ,
                          @strFunctionID      = @strFunctionID      ,
                          @intPatternID            = @intPatternID            ,
                          @strSymbol            = @strSymbol            ,
                          @strComments            = @strComments            ,
                          @strCallingProgram      = 'pCalcTwiggs_250'
               END

-- * * * * * * * * * * *   T E s t
SET @strTwiggsCalcTEMP = CAST(@currTwiggsCalc_250 AS CHAR(1000)) -- temp
SET @strCurrentTwiggsVolumeTEMP = CAST(@intCurrentTwiggsVolume as CHAR)
-- * * * * * * * * * * * * *

/*
-- No @intCurrentTwiggsVolume
            INSERT INTO dbo.tblTestWrite
              (Symbol, EntryDate, Volume)
            VALUES
              (@strSymbol, @datTodaysDate, @intCurrentTwiggsVolume)
*/
            SET @currLastTwiggsVolumeTEMP = CONVERT(MONEY, @intCurrentTwiggsVolume)
            INSERT INTO dbo.tblTwiggsMF
              (Symbol, EntryDate, Volume)
              VALUES
>>here               (@strSymbol, @datTodaysDate, @currLastTwiggsVolumeTEMP)

==================  Below is table tblTwiggsMF ============================
DROP PROCEDURE pCreate_tblTwiggsMF
go

CREATE PROCEDURE pCreate_tblTwiggsMF
AS
 
  IF EXISTS
    (
      SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
      WHERE TABLE_NAME = 'tblTwiggsMF'
     )
      DROP TABLE dbo.tblTwiggsMF
  CREATE TABLE dbo.tblTwiggsMF
    (
     RecID            bigint      IDENTITY      NOT NULL,
     Symbol                  varchar(7)      NOT NULL
        FOREIGN KEY REFERENCES dbo.tblSecurity(Symbol),
        CONSTRAINT tblTwiggsMF_PK PRIMARY KEY
            (RecID, Symbol, EntryDate),
     EntryDate                  smalldatetime      NOT NULL      DEFAULT GETDATE(),
     Volume                  money            NOT NULL      DEFAULT 0,
--     Volume                  bigint            NOT NULL      DEFAULT 0,
     Active                  tinyint            NOT NULL      DEFAULT 1
    )


Return
Start Free Trial
[+][-]08.05.2008 at 04:09AM PDT, ID: 22159760

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.05.2008 at 07:22PM PDT, ID: 22166742

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.05.2008 at 09:11PM PDT, ID: 22167076

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.05.2008 at 09:42PM PDT, ID: 22167172

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: Microsoft, SQL Server, 2000
Sign Up Now!
Solution Provided By: fesnyng
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08.06.2008 at 08:00PM PDT, ID: 22177055

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628