存储过程因 "Arithmetic overflow error converting varchar to data type numeric." 错误而失败,即使我明确地转换了所有字段
stored procedure failing with "Arithmetic overflow error converting varchar to data type numeric." error even though I cast all fields explicitly
我花了过多的时间来让这个存储过程工作,它基本上是将查询结果转换成一种 html 格式,然后通过电子邮件发送。 proc 从中收集数据的查询工作正常并且 returns 结果符合预期,但是当尝试将输出放入电子邮件的 table 时,它因算术溢出错误而失败。
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
查询结果如下:
ORDDOCID ITEMNMBR (No column name) SerialsScanned Diff
T100742 APP-MU8X2LL/A 100 0 100
任何指导将不胜感激。
在失败的行中你基本上有:
declare @sITTQty numeric(19,5); -- = ...
declare @sITT varchar(max); -- = ...
set @sITT = @sITT
+ '</td><td style="border-style: solid; border-width: thin">'
+ @sITTQty
-- + ...
所以您正在尝试将数字添加到 varchar。这会尝试将 varchar 数据提升为数字,这就是您收到错误的原因。
向 @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))
-- + ...
编辑:您必须对 @sDiff
和 @sSrlQty
执行相同的操作
我花了过多的时间来让这个存储过程工作,它基本上是将查询结果转换成一种 html 格式,然后通过电子邮件发送。 proc 从中收集数据的查询工作正常并且 returns 结果符合预期,但是当尝试将输出放入电子邮件的 table 时,它因算术溢出错误而失败。
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
查询结果如下:
ORDDOCID ITEMNMBR (No column name) SerialsScanned Diff
T100742 APP-MU8X2LL/A 100 0 100
任何指导将不胜感激。
在失败的行中你基本上有:
declare @sITTQty numeric(19,5); -- = ...
declare @sITT varchar(max); -- = ...
set @sITT = @sITT
+ '</td><td style="border-style: solid; border-width: thin">'
+ @sITTQty
-- + ...
所以您正在尝试将数字添加到 varchar。这会尝试将 varchar 数据提升为数字,这就是您收到错误的原因。
向 @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))
-- + ...
编辑:您必须对 @sDiff
和 @sSrlQty