下一个自动生成的ID无故失败? ISNULL(MAX(IdDWUpdate),0)

时间:2014-05-23 10:14:40

标签: sql-server sql-server-2008 tsql ssis

我有一个SQL Server 2008和SSIS包,到目前为止(一年左右)一直运行良好。从几天前开始,它会在错误时停止:

Description: Cannot insert the value NULL into column 'InvoiceIdDW', table 'Test2.dbo.Payments'; 
column does not allow nulls. INSERT fails.

然而,它失败的脚本部分已经起作用,它应该通过在NULL值上给出0来处理NULL。该脚本自上次成功运行以来未被更改

INSERT INTO [Testi2].[dbo].[Payments]
           ([PaymentId]
           ,[PaymentsArchiveId]
           ,[SourceSystemIdDW]
           ,[PaymentType]
           ,[InvoiceIdDW]
           ,[PaymentVoucherId]
           ,[PaymentDate]
           ,[PaymentAmount]
           ,[CurrencyCode]
           ,[PaymentAmountCurrency])
SELECT SuLasNro , SuArkTunn , 1 , SuLaji , ISNULL(h.InvoiceIdDW,0) , s.SuTosNro , s.SuPvm , s.SuMk , s.SuValKdi , s.SuVal
FROM Latausalue.dbo.Tikon_suoritukset s
    LEFT OUTER JOIN Testi2.dbo.Company c ON c.SourceSystemIdDW = 1 AND c.CompanyId = s.yritys
    LEFT OUTER JOIN Testi2.dbo.InvoiceHeader h ON h.InvoiceId = s.SuLasNro AND h.CompanyIdDW = c.CompanyIdDW AND h.OrigKey_1 = s.SuAsTunn AND h.OrigKey_2 = s.SuSVTili AND h.SourceSystemIdDW = 1
UNION ALL
SELECT  s.invoicepayment_id , s.invoicepayment_archive , 3 , 'M' , h.InvoiceIdDW , '' , s.invoicepayment_date , 
        s.invoicepayment_total , s.invoicepayment_account_currency , s.invoicepayment_total_incurrency
FROM Latausalue.dbo.Lemon_suoritukset s
    LEFT OUTER JOIN Testi2.dbo.Company c ON c.SourceSystemIdDW = 3 AND c.CompanyId = RIGHT('0000' + s.yritys,4)
    LEFT OUTER JOIN Testi2.dbo.InvoiceHeader h ON h.InvoiceId = s.invoice_id AND h.CompanyIdDW = c.CompanyIdDW AND h.SourceSystemIdDW = 3
WHERE h.InvoiceIdDW IS NULL

语法有什么问题?

1 个答案:

答案 0 :(得分:0)

问题最可能的原因是UNION ALL之后的'select'语句,如@NickyvV所述。

SELECT  s.invoicepayment_id , s.invoicepayment_archive , 3 , 'M' , h.InvoiceIdDW

更重要的是,有一个条件:

 WHERE h.InvoiceIdDW IS NULL

您永远不能依赖该套餐1年'没问题'生产意味着一切都好。 随时准备:)