在sp_executesql用法中将int转换为数据类型numeric的算术溢出错误

时间:2014-01-23 03:21:14

标签: sql-server-2008-r2

您好我正在尝试获取一些小数的计算值,如下所示 -

BEGIN
 declare @b_server_qty int,
 @b_bookings numeric(30,4),
 @b_std_margin numeric(30,4),
 @b_avg_sp numeric(30,4),
 @b_avg_discount numeric(30,4),
 @c_server_qty int,
 @pos_ucs_qty int,
 @qry_B_server_qty nvarchar(max)='',
 @qry_C_server_qty nvarchar(max)='',
 @paramDefinition nvarchar(max)

 SELECT @qry_B_server_qty = N'SELECT @b_server_qtyOUT =        sum(server_qty),@b_bookingsOUT = SUM(product_bookings_net)/1000000,
    @b_std_marginOUT = isnull(((sum(product_bookings_net)-sum(actaul_product_cost))/nullif(sum(product_bookings_net),0)),0),
    @b_avg_spOUT = isnull((sum(product_bookings_net)/nullif(sum(server_qty),0)),0),
    @b_avg_discountOUT = isnull(((sum(product_bookings_base_list)-sum(product_bookings_net))/nullif(sum(product_bookings_base_list),0)),0) 
    FROM UCS_Bizz_PRODUCT_DATA 
    WHERE '+@common_Where_clause+' AND major_line_product_family = ''UCSB'' 
    OR (major_line_product_family = ''BKG_ADJ_PF'' AND product_family=''UCSB'')';
 SET @paramDefinition = N'@b_server_qtyOUT INT OUTPUT, @b_bookingsOUT numeric(30,4) OUTPUT, 
 @b_std_marginOUT numeric(30,4) OUTPUT,@b_avg_spOUT numeric(30,4) OUTPUT,@b_avg_discountOUT numeric(30,4) OUTPUT';
 EXEC sp_executesql @qry_B_server_qty,@paramDefinition,@b_server_qtyOUT=@b_server_qty OUTPUT,
      @b_bookingsOUT =@b_bookings OUTPUT, @b_std_marginOUT =@b_std_margin OUTPUT,@b_avg_spOUT =@b_avg_sp OUTPUT,
      @b_avg_discountOUT=@b_avg_discount OUTPUT;

 SELECT @b_std_margin,@b_avg_sp,@b_avg_discount,@b_bookings

正确计算值

(SELECT @b_std_margin,@b_avg_sp,@b_avg_discount,@b_bookings

此语句在执行过程后正确打印值)但仍然出现错误 -

"Arithmetic overflow error converting int to data type numeric".

我该如何解决这个问题?我已经验证,我计算字段的长度肯定不会超过30位。

1 个答案:

答案 0 :(得分:3)

当我们传递一个太大的数值时,通常会发生错误。如果我们声明一个数字(5,2)变量,它只能在小数点前保持三位数。

以下语句在SQL Server中正常工作:

DECLARE @n NUMERIC(5,2)
SET @n = 123.123456

但是,设置变量值时我们会遇到相同的错误,如下所示:

DECLARE @n NUMERIC(5,2)
SET @n = 1234.123456