сбой хранимой процедуры: «Ошибка переполнения Arithmeti c при преобразовании varchar в тип данных numeri c.» ошибка, хотя я явно указывал все поля - PullRequest
0 голосов
/ 14 июля 2020

Я потратил слишком много времени на то, чтобы заставить работать этот сохраненный pro c, он в основном переводит результаты запроса в формат html, который отправляется по электронной почте. Запрос, из которого pro c собирает данные, отлично работает и возвращает ожидаемые результаты, однако при попытке поместить вывод в таблицу для электронного письма происходит сбой с ошибкой переполнения arithmeti c.

    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

Любые указания будут чрезвычайно признательны.

1 Ответ

1 голос
/ 14 июля 2020

В строке с ошибкой у вас, по сути, есть:

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

Итак, вы пытаетесь добавить numeri c к varchar. Это пытается продвинуть данные varchar до numeri c, и именно поэтому вы получаете сообщение об ошибке.

Добавьте явное приведение к @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

...