Sql 发生算术溢出。在执行存储过程时

Sql 发生算术溢出。在执行存储过程时,sql,stored-procedures,sybase,Sql,Stored Procedures,Sybase,你好 我有以下存储过程 CREATE PROCEDURE dbo.glextract_branch_audit_figures (@fin_period DATETIME, @runtime VARCHAR(1)) AS BEGIN declare @branch INT declare @debtor_open_balance money declare @debtor_theo_balance money declare @debtor_close_balance money decl

你好 我有以下存储过程

CREATE PROCEDURE dbo.glextract_branch_audit_figures (@fin_period DATETIME, @runtime VARCHAR(1))
AS 

BEGIN

declare @branch INT
declare @debtor_open_balance money
declare @debtor_theo_balance money
declare @debtor_close_balance money
declare @stock_open_balance money
declare @stock_close_balance money
declare @mtd_goods_in money
declare @mtd_goods_out money
declare @mtd_adjustments money
declare @mtd_sales money
declare @stock_theo_balance money

SELECT @branch = branch FROM branch_control

SELECT @debtor_open_balance = debtor_balance, @stock_open_balance = (stock_balance - stock_ind_bf + stock_onrepr_bf) FROM period_control WHERE fin_period = @fin_period

SELECT @debtor_theo_balance = @debtor_open_balance + (SELECT SUM(doc_amt) from cf_debtor_transaction WHERE tran_type IN 
          (4002, 4004, 4050, 4052, 4099, 4101, 4102, 4103, 4104, 4105, 4106, 4107, 4108, 4109, 4110, 4111, 4112, 4113, 4114, 4115,         
               4118, 4119, 4123, 4201, 4204, 4211, 4212, 4220, 4225, 4227, 4229, 4232, 4240, 4241, 4243, 4244, 4248, 4249, 4250, 4251, 4275, 
           4300, 4423, 4502, 4551, 4552, 4599, 4601, 4602, 4603, 4604, 4605, 4606, 4607, 4608, 4609, 4611, 4612, 4613, 4614, 4615, 4616, 
           4620, 4621, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4713, 4714, 4715, 4716, 4717, 4718, 4719, 
           4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4740, 4741, 4742, 4743, 4744, 4750, 4751, 4774, 4777) 
           AND fin_period = @fin_period)

SELECT @debtor_close_balance = SUM(dbo.cf_acc_balance(account_guid)) FROM account

SELECT @stock_close_balance = SUM((sh.qty_on_hand - sh.qty_on_ind + sh.qty_on_repair) * a.cost) FROM stock_holding  sh, all_sku_costs a WHERE a.sku = sh.sku

SELECT @mtd_goods_in = ISNULL(SUM(CASE WHEN tran_type IN (7215, 7225, 7715, 7717, 7718, 7805, 7405, 7705, 7707, 7708) THEN 0 ELSE cost * qty END),0)
                FROM stock_transaction 
                WHERE tran_type IN (7015, 7035, 7045, 7105, 7115, 7125, 7185, 7135, 7145, 7155, 7205, 7215, 7225, 7235,
                    7245, 7255, 7265, 7275, 7285, 7295, 7405, 7415, 7445, 7455, 7465, 7475, 7485, 7495,
                    3502, 4599, 7955, 7305, 7335, 7175, 7195)
                AND fin_period = @fin_period

SELECT @mtd_goods_out = ISNULL(SUM(CASE WHEN tran_type IN (7215, 7225, 7715, 7717, 7718, 7805, 7405, 7705, 7707, 7708) THEN 0 ELSE (cost * qty) * -1 END),0) 
                FROM stock_transaction 
                WHERE tran_type IN (7505, 7515, 7535, 7605, 7615, 7625, 7685, 7635, 7645, 7655, 7665, 7705, 7715, 7725,
                    7735, 7745, 7755, 7765, 7795, 7805, 7815, 7845, 7855, 7865, 7885, 7895, 7875, 7925,
                    3001, 3002, 4099, 7956, 7775, 7935, 7675, 7695)                 
                AND fin_period = @fin_period

SELECT @mtd_adjustments = ISNULL(SUM(CASE WHEN tran_type IN (7717,7718,7707,7708) THEN 0
                    ELSE CASE WHEN tran_type IN (7018,7048,7718,7829,7868) THEN (cost * qty) * -1
                    ELSE (cost * qty) END END),0)
                FROM stock_transaction 
                WHERE tran_type in (7017, 7018, 7037, 7038, 7047, 7048, 7107, 7108, 7117, 7118, 7127, 7128, 7137, 7138,
                    7147, 7148, 7207, 7208, 7217, 7218, 7227, 7228, 7427, 7437, 7447, 7448, 7497, 7507,
                    7508, 7517, 7518, 7537, 7538, 7647, 7648, 7707, 7708, 7717, 7718, 7828, 7838, 7868,
                    7898, 7438, 7417, 7818, 7957, 7958, 7967, 7968, 7428, 7438, 7829, 7839)
                AND fin_period = @fin_period

SELECT @mtd_sales = ISNULL(SUM(cost * -1),0) FROM debtor_transaction WHERE tran_type IN (4002,4502) AND fin_period = @fin_period   

SELECT @stock_theo_balance = @stock_open_balance + @mtd_goods_in + @mtd_goods_out + @mtd_adjustments + @mtd_sales                                           

 IF (@runtime = 'E')
 BEGIN 
     TRUNCATE TABLE GL_WK_BRANCH_AUDIT_FIGURES
    INSERT INTO GL_WK_BRANCH_AUDIT_FIGURES


 SELECT
    @fin_period as Period,
    @branch as BranchCode,
    ISNULL(@debtor_open_balance,0) as DebtorsOpening,
    ISNULL(@debtor_close_balance,0) as DebtorsClosing,
    ISNULL(@stock_open_balance,0) as StockOpening,
    ISNULL(@stock_close_balance,0) as StockClosing,
    ISNULL(@debtor_theo_balance,0) - ISNULL(@debtor_close_balance,0) as DebtorsDiff,
    ISNULL(@stock_theo_balance,0) - ISNULL(@stock_close_balance,0) as StockDiff
    END
    ELSE
    BEGIN 
 TRUNCATE TABLE GL_BRANCH_AUDIT_FIGURES
 INSERT INTO GL_BRANCH_AUDIT_FIGURES
    SELECT
    @fin_period as Period,
    @branch as BranchCode,
    ISNULL(@debtor_open_balance,0) as DebtorsOpening,
    ISNULL(@debtor_close_balance,0) as DebtorsClosing,
    ISNULL(@stock_open_balance,0) as StockOpening,
    ISNULL(@stock_close_balance,0) as StockClosing,
    ISNULL(@debtor_theo_balance,0) - ISNULL(@debtor_close_balance,0) as DebtorsDiff,
    ISNULL(@stock_theo_balance,0) - ISNULL(@stock_close_balance,0) as StockDiff
    END
END
我试图执行如下所示的过程,传递fin period和runtime参数

execute dbo.glextract_branch_audit_figures'2018-08-05', 'E'
然而,它给我的是:算术溢出发生错误

我有一个股票交易表,它有一个名为doc\u amt的字段,金额很大。 如果我把这个数值改小一些,它不会因为上面的错误而崩溃

请参见以下股票交易的价值:

请参阅上述步骤

SELECT @mtd_goods_out

此处列出了货币数据类型的最大大小-如果您违反该大小,您将获得艺术溢出。您唯一的选择是使用特定的数字数据类型,它允许更大的大小:


单据金额列的数据类型是什么?@SalmanA数据类型是money