Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 3rd, 2006, 11:47 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Odd arithmetic overflow error....

Hi. I'm getting a "Arithmetic overflow error converting numeric to data type numeric." error on an old stored procedure of ours. I debugged the code with print statements to see where it was happening and it doesn't seem to make any sense why we are getting the error. The data types match and the values do not overflow the attribute parameters.

The specific location where the error occurs is when we try to move data from a temp table to the actual physical table. I commented the location.

Here is the SQL:
--------------------------------------------------
declare @dtAsOfdate DATETIME
set @dtAsOfDate = '2006-04-16';

  DECLARE @RC INTEGER
-------------------------
-- 1) Eligible Investments:
-------------------------

-- Input: @SPVId - SPV we are running process for
-- @Yes - value of enum CCPEnum::eYesNoYes (get by lookup).

-- Output: Recordset (temp table) of Collaterals that are eligible for MV Test (#MVTriggerInvestments).

   DECLARE @Yes INTEGER
   EXEC @RC = [dbo].CPLookupVal 'YesNo', 'Yes', @Yes OUTPUT
   IF (@RC<>0)BEGIN
      RAISERROR ('SP_OCCalculationMVTriggerTest: Failed to find Yes enum', 16, 1) WITH SETERROR
   END
drop table #MVTriggerInvestments
    BEGIN

        SELECT dbal.SPVId,
               dbal.CusipId,
               dbal.GroupId,
               @dtAsOfDate AS AsOfDate,
               dbal.NormalOCRate,
               dbal.SteppedUpOCRate,
               dbal.AllocMarketValue AS MarketValue,
               dbal.NbrDays,
               dbal.PriceChangeRatio

        INTO #MVTriggerInvestments

        FROM DailyCollateralBalance dbal

        JOIN CollateralGroupIncludeInOC gin
            ON dbal.SPVId = 2
            AND gin.SPVId = 2
            AND dbal.AsOfDate = '2006-04-16'
            AND @dtAsOfDate BETWEEN gin.EffectiveFrom AND gin.EffectiveTo
            AND dbal.GroupId = gin.GroupId
            AND gin.IncludeInOC = @Yes

    END
select * from #MVTriggerInvestments
print 'end #1'
--select * from #MVTriggerInvestments --looks ok

--------------------------------------------------------------
-- 2) Calculate Weighted Average Price change ratio Market Value (by Group):
-- PCRMV - Price Change Ratio Market Value
--------------------------------------------------------------

-- Input : Recordset of collaterals (having New/Old prices, MarketValue defined)
-- Output: Recordset Aggregated by Group (#GroupOCRate)
drop table #MVTriggerGroup
   BEGIN
       SELECT A.SPVId,
              A.AsOfDate,
              A.GroupId,
              A.NormalOCRate,
              A.SteppedUpOCRate,
              A.MarketValue,

              [dbo].fn_divide_or_number (B.PriceChangeRatioMarketValue, B.MarketValueForPeriod, 0.00) as PriceChangeRatio,

              CAST (0 AS NUMERIC(12,9)) AS OCRate,
              CAST ('' AS VARCHAR(6)) AS OCRateType,
              CAST (0 AS NUMERIC(18,2)) AS DiscMarketValue,
              CAST (0 AS NUMERIC(18,2)) AS InterestAccrued

       INTO #MVTriggerGroup

       FROM
       (
           SELECT SPVId,
                  AsOfDate,
                  GroupId,
                  NormalOCRate,
                  SteppedUpOCRate,
                  SUM(MarketValue) AS MarketValue

           FROM #MVTriggerInvestments
           GROUP BY SPVId, AsOfDate, GroupId, NormalOCRate, SteppedUpOCRate
       ) A --works up to here

       JOIN
       (SELECT SPVId,
               SUM(AllocMarketValue) AS MarketValueForPeriod,
               SUM(AllocMarketValue * PriceChangeRatio) as PriceChangeRatioMarketValue,
               GroupId

        FROM T_DailyCollateralBalance
        WHERE SPVId = 2
        AND AsOfDate between '2006-03-17' and '2006-04-15'
        AND IsBusinessDay = 1
        GROUP BY SPVId, GroupId
       ) B

       ON A.SPVId = B.SPVId
       AND A.GroupId = B.GroupId

   END
print 'end #2'
---------------------------------------------
-- Calculate OCRate to apply for each group.
---------------------------------------------
   BEGIN
       UPDATE #MVTriggerGroup
       SET OCRate = (CASE WHEN ((PriceChangeRatio < 0) AND ABS(PriceChangeRatio) > (0.55 * NormalOCRate)) THEN SteppedUpOCRate
                          ELSE NormalOCRate
                     END),
           OCRateType = (CASE WHEN ((PriceChangeRatio < 0) AND ABS(PriceChangeRatio) > (0.55 * NormalOCRate)) THEN 'stepup'
                              ELSE 'normal'
                         END)
   END
print 'end #3'
   -------------------------------------
   -- Calculate discounted Market Value
   -------------------------------------
   UPDATE #MVTriggerGroup
   SET DiscMarketValue = MarketValue / (1.0 + OCRate * 0.01)
print 'end #4'
   ---------------------------------
   -- Insert data from temp tables
   ---------------------------------
   -- 1)
select * from #MVTriggerInvestments

print 'begin tran'
   BEGIN TRAN
       DELETE T_MVTriggerInvestments
       WHERE SPVId = 2 AND AsOfDate = '2006-04-16'
print 'DELETE T_MVTriggerInvestments'
--error is here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!
       INSERT T_MVTriggerInvestments
          (
               SPVId ,
               CusipId ,
               GroupId ,
               AsOfDate ,
               NormalOCRate ,
               SteppedUpOCRate ,
               MarketValue ,
               NbrDays ,
               OldPrice ,
               NewPrice ,
               PriceChangeRatio
          )
       SELECT SPVId ,
               CusipId ,
               GroupId ,
               AsOfDate ,
               NormalOCRate ,
               SteppedUpOCRate ,
               MarketValue ,
               NbrDays ,
               0.00 ,
               0.00 ,
               PriceChangeRatio

       FROM #MVTriggerInvestments
print 'end mvtriggerinv select'
   COMMIT TRAN
--end error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!
   -- 2)
print 'begin tran 2'
   BEGIN TRAN
      DELETE T_OCTestGroup
      WHERE SPVId = 2 AND AsOfDate = '2006-04-16'

      INSERT T_OCTestGroup
      (
        SPVId ,
        AsOfDate ,
        GroupId ,
        NormalOCRate ,
        SteppedUpOCRate ,
        MarketValue ,
        PriceChangeRatio,
        OCRate ,
        OCRateType ,
        DiscMarketValue ,
        InterestAccrued ,
        SectionA ,
        CPFace ,
        IntExpense ,
        Fees ,
        SectionB ,
        Receivables ,
        IntReceivables ,
        CashBalance ,
        Investments ,
        SectionC ,
        ExcessCollateral,
        MaxCPAllowed
      )
      SELECT
            SPVId ,
            AsOfDate ,
            GroupId ,
            NormalOCRate ,
            SteppedUpOCRate ,
            MarketValue ,
            PriceChangeRatio,
            OCRate ,
            OCRateType ,
            DiscMarketValue ,
            InterestAccrued ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0 ,
            0

      FROM #MVTriggerGroup
print 'end tran 2'
   COMMIT TRAN
--------------------------------------------------

Reply With Quote
  #2 (permalink)  
Old May 3rd, 2006, 11:51 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks in advance for your assistance. I'm not a SQL developer by nature so forgive me if its a dumb mistake in some old code.

Reply With Quote
  #3 (permalink)  
Old May 12th, 2006, 12:05 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a couple of links that discusses the possible causes of this error and how to solve it or work around it:

http://www.sql-server-helper.com/faq...sages-p09.aspx
http://www.sql-server-helper.com/err.../msg-8115.aspx

SQL Server Helper
http://www.sql-server-helper.com
Reply With Quote
  #4 (permalink)  
Old May 12th, 2006, 07:54 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Table #MVTriggerGroup does not have a CusipId column!

When you try to do an INSERT with
--------------------------------------------------------
  INSERT T_MVTriggerInvestments
          (
               SPVId ,
               CusipId ,
               GroupId ,
               AsOfDate ,
               NormalOCRate ,
               SteppedUpOCRate ,
               MarketValue ,
               NbrDays ,
               OldPrice ,
               NewPrice ,
               PriceChangeRatio
          )
       SELECT SPVId ,
               CusipId ,
               GroupId ,
               AsOfDate ,
               NormalOCRate ,
               SteppedUpOCRate ,
               MarketValue ,
               NbrDays ,
               0.00 ,
               0.00 ,
               PriceChangeRatio
       FROM #MVTriggerInvestments
--------------------------------------------------------
There is no CusipID column because you created the temp table with
--------------------------------------------------------
       SELECT A.SPVId,
              A.AsOfDate,
              A.GroupId,
              A.NormalOCRate,
              A.SteppedUpOCRate,
              A.MarketValue,
              [dbo].fn_divide_or_number (B.PriceChangeRatioMarketValue, B.MarketValueForPeriod, 0.00) as PriceChangeRatio,
              CAST (0 AS NUMERIC(12,9)) AS OCRate,
              CAST ('' AS VARCHAR(6)) AS OCRateType,
              CAST (0 AS NUMERIC(18,2)) AS DiscMarketValue,
              CAST (0 AS NUMERIC(18,2)) AS InterestAccrued
       INTO #MVTriggerGroup

       FROM
             bla bla bla
--------------------------------------------------------



Reply With Quote
  #5 (permalink)  
Old May 12th, 2006, 09:17 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks gentlemen. I figured it out already. I had already read all I could regarding syntax issues so those links were old news already.

It was indeed a data problem. I had misinterpreted how precision and scale worked and had a numeric(12,9) value that was more like a numeric(13,9) :)

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple arithmetic overflow vbscript handy Classic ASP Basics 32 March 31st, 2009 04:25 PM
Arithmetic overflow error gregalb Reporting Services 1 June 24th, 2008 02:42 PM
arithmetic overflow problem flyfish SQL Server 2000 4 July 12th, 2005 04:37 PM
Arithmetic overflow error converting expression to sinapra Classic ASP Databases 8 September 3rd, 2004 12:24 AM
Arithmetic overflow error converting expression to spikey SQL Server 2000 1 June 24th, 2003 07:19 PM



All times are GMT -4. The time now is 09:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.