简体   繁体   English

存储过程因“将 varchar 转换为数字数据类型的算术溢出错误”而失败。 即使我明确地转换所有字段也会出错

[英]stored procedure failing with “Arithmetic overflow error converting varchar to data type numeric.” error even though I cast all fields explicitly

I've spent an inordinate amount of time on getting this one stored proc to work, it's basically putting results from the query into an html format that is getting emailed.我花了很多时间来让这个存储过程正常工作,它基本上是将查询的结果放入通过电子邮件发送的 html 格式。 The query that the proc collects data from works fine and returns results as expected however when the output is attempted to be put into the table for the email it fails with the arithmetic overflow error. proc 从中收集数据的查询工作正常并按预期返回结果,但是当尝试将 output 放入 email 的表中时,它会因算术溢出错误而失败。

    DECLARE @sCompanyName varchar(50) = 'ITT Discrepancy' 
    DECLARE @sEmailTo varchar(128) = '' 
    DECLARE @sMailProfile varchar(128) = 'Database Notification Profile'

    -- Other declarations
    DECLARE @sEMailSubject varchar(100) = @sCompanyName + convert(varchar(10),Convert(date,getdate()))
    DECLARE @sMessageBody varchar(max) = '', @sITT varchar(max) = '', @iMailItemID int = 0
    
    -- Exit if there are no records to process
    IF NOT EXISTS(select ORDDOCID, ITEMNMBR, TRNSFQTY, case when ct is null then 0 else ct end as 'SerialsScanned', TRNSFQTY-ct as 'Diff' from
        (select ORDDOCID, ITEMNMBR,TRNSFQTY from ZTEST.dbo.SVC00701 where TRNSFLOC = 'TS-PF' and STATUS in (3,4)) as gp
        left join
        (select OrderNumber, ItemNumber, COUNT(serial) as 'ct' from ManualScan.dbo.SoldItems where TranType = 'ITT' group by OrderNumber, ItemNumber) as srl on gp.ORDDOCID = srl.OrderNumber and gp.ITEMNMBR = srl.ItemNumber
        where TRNSFQTY-(case when ct is null then 0 else ct end)>0)
        RETURN
        
    -- Set the header
    SET @sMessageBody = '<html><head><style type="text/css">
                                    .style1
                                    {
                                        width: 100%;
                                        border-style: solid;
                                        border-width: 1px;
                                    }
                                </style>
                        </head>
                        <p style="font-size: large; color: #CC3300;">
                            ' + @sCompanyName + '
                        </p>'
    
    -- Set the Items Received
    DECLARE @sITTnum varchar(31), @sItemNum varchar(31), @sITTQty numeric(19,5), @sSrlQty numeric(18,0), @sDiff numeric(18,0)
    DECLARE cITT CURSOR FOR 
        select ORDDOCID, ITEMNMBR, cast(TRNSFQTY as numeric(18,0)), cast(case when ct is null then 0 else ct end as numeric(18,0)) as 'SerialsScanned', cast(TRNSFQTY as numeric(18,0))-cast(case when ct is null then 0 else ct end as numeric(18,0)) as 'Diff' 
        from
        (select ORDDOCID, ITEMNMBR,cast(TRNSFQTY as numeric(18,0)) as 'TRNSFQTY' from ZTEST.dbo.SVC00701 where TRNSFLOC = 'TS-PF' and STATUS in (3,4)) as gp
        left join
        (select OrderNumber, ItemNumber, cast(case when COUNT(serial) is null then 0 else COUNT(serial) end as numeric(18,0)) as 'ct' from ManualScan.dbo.SoldItems where TranType = 'ITT' group by OrderNumber, ItemNumber) as srl on gp.ORDDOCID = srl.OrderNumber and gp.ITEMNMBR = srl.ItemNumber
        where TRNSFQTY-(case when ct is null then 0 else ct end)>0
    OPEN cITT
    FETCH NEXT FROM cITT INTO @sITTnum, @sItemNum, @sITTQty, @sSrlQty, @sDiff
    WHILE @@FETCH_STATUS = 0
    BEGIN
`it says error occurs at this line...`
        SELECT @sITT = @sITT + '<tr><td style="border-style: solid; border-width: thin">' + @sITTnum +
                                        '</td><td style="border-style: solid; border-width: thin">' + @sItemNum +
`but if I comment out from these lines it works fine so something in these values/variables` 
                                        '</td><td style="border-style: solid; border-width: thin">' + @sITTQty + 
                                        '</td><td style="border-style: solid; border-width: thin">' + @sSrlQty + 
                                        '</td><td style="border-style: solid; border-width: thin">' + @sDiff + 
'</td></tr>'
        FETCH NEXT FROM cITT INTO @sITTnum, @sItemNum, @sITTQty, @sSrlQty, @sDiff
    END
    CLOSE cITT
    DEALLOCATE cITT
    
    IF @sITT <> ''
        SET @sITT = '<table class="style2" 
                            style="font-family: "Times New Roman", Times, serif; border-style: solid; border-width:thin">
                            <tr>
                                <td style="border-style: solid; border-width: thin" >
                                    <b>ITT Num</b></td>
                                <td style="border-style: solid; border-width: thin" >
                                    <b>Item Number</b></td>
                                <td style="border-style: solid; border-width: thin">
                                    <b>ITT Quantity</b></td>
                                <td style="border-style: solid; border-width: thin">
                                    <b>Scanned Quantity</b></td>
                                <td style="border-style: solid; border-width: thin">
                                    <b>Difference</b></td>
                            </tr>' + @sITT + '</table></body></html>'
    SET @sMessageBody = @sMessageBody + @sITT
    
    EXEC msdb..sp_send_dbmail 
        @profile_name   = @sMailProfile, 
        @body_format    = 'HTML', 
        @recipients     = @sEmailTo, 
        @subject        = @sEMailSubject, 
        @body           = @sMessageBody, 
        @mailitem_id    = @iMailItemID OUTPUT

These are the results of the query:这些是查询的结果:

ORDDOCID        ITEMNMBR       (No column name) SerialsScanned  Diff
T100742         APP-MU8X2LL/A       100                0         100

Any guidance would be extremely appreciated.任何指导将不胜感激。

In the failing line you essentially have:在失败的行中,您基本上拥有:

declare @sITTQty numeric(19,5); -- = ...
declare @sITT varchar(max); -- = ...
set @sITT = @sITT  
 + '</td><td style="border-style: solid; border-width: thin">' 
 + @sITTQty 
 -- + ...

So you are trying to add a numeric to a varchar.因此,您正在尝试将数字添加到 varchar。 This attempts to promote the varchar data to numeric, and that is why you are getting the error.这试图将 varchar 数据提升为数字,这就是您收到错误的原因。

Add an explicit cast to @sITTQty@sITTQty添加显式强制转换

declare @sITTQty numeric(19,5);
declare @sITT varchar(max);
set @sITT = @sITT 
 + '</td><td style="border-style: solid; border-width: thin">' 
 +  cast(@sITTQty as varchar(19)) 
 -- + ...

Edit: You will have to do the same with @sDiff and @sSrlQty编辑:您必须对@sDiff@sSrlQty执行相同的操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储过程错误算术溢出错误将数字转换为数据类型 varchar - Stored Procedure Error Arithmetic overflow error converting numeric to data type varchar 在存储过程(参数化)计算中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in the stored procedure (parameterised) caluculation 错误:将数字转换为数据类型varchar的算术溢出错误 - Error : Arithmetic overflow error converting numeric to data type varchar 存储过程算术溢出错误将表达式转换为数据类型int - Stored Procedure Arithmetic overflow error converting expression to data type int 存储过程-将数据类型varchar转换为数值时出错 - Stored procedure - Error converting data type varchar to numeric SQLServerException:算术溢出错误,将数值转换为数值类型的数值 - SQLServerException: Arithmetic overflow error converting numeric to data type numeric 将数据类型nvarchar转换为数字时出错。 在过程中调用函数时 - Error converting data type nvarchar to numeric. when calling function inside procedure 在存储过程中将 varchar 转换为数字时出错 - Error converting varchar to numeric in Stored Procedure 在我的 SQL Server 存储过程中将数据类型 varchar 转换为数字时出错 - Error converting data type varchar to numeric in my SQL Server stored procedure 将数据类型varchar转换为存储过程中的int错误时出错 - Error converting data type varchar to int error in stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM