Question

For some reason, this is giving me the "cannot insert duplicate record into table" error.

INSERT  INTO [DMS].[dbo].[Deductions]
        (
         CustomerID,
         DeductionCode,
         DeductionDescription
        )
        SELECT  b.CustomerID,
                b.AdjustmentReason,
                b.AdjustmentReason
        FROM    @CreditDebitAdjustmentDetail b
        WHERE   NOT EXISTS ( SELECT 1
                             FROM   [DMS].[dbo].[Deductions]
                             WHERE  CustomerID = b.CustomerID
                                    AND DeductionCode = b.AdjustmentReason )

The weird thing is, I tested it as such:

DECLARE @CreditDebitAdjustmentDetail TABLE
        (
         CustomerID INT,
         AdjustmentReason VARCHAR(50)
        )

INSERT  INTO @CreditDebitAdjustmentDetail
        (CustomerID, AdjustmentReason)
VALUES  (143, -- CustomerID - int
         '024'  -- AdjustmentReason - varchar(50)
         )

INSERT  INTO [DMS].[dbo].[Deductions]
        (
         CustomerID,
         DeductionCode,
         DeductionDescription
        )
        SELECT  b.CustomerID,
                b.AdjustmentReason,
                b.AdjustmentReason
        FROM    @CreditDebitAdjustmentDetail b
        WHERE   NOT EXISTS ( SELECT 1
                             FROM   [DMS].[dbo].[Deductions]
                             WHERE  CustomerID = b.CustomerID
                                    AND DeductionCode = b.AdjustmentReason )

And it DOES NOT insert into the table because the record already exists.

Am I missing something here?

EDIT - I thought I had fixed it by doing this but I'm still getting the same error:

INSERT  INTO [DMS].[dbo].[Deductions]
        (
         CustomerID,
         DeductionCode,
         DeductionDescription
        )
        SELECT  a.CustomerID,
                a.AdjustmentReason,
                a.AdjustmentReason
        FROM    @CreditDebitAdjustmentDetail a
        WHERE   NOT EXISTS ( SELECT 1
                             FROM   [DMS].[dbo].[Deductions] b
                             WHERE  a.CustomerID = b.CustomerID
                                    AND a.AdjustmentReason = b.DeductionCode )
Was it helpful?

Solution

I figured it out, DOH!

KEYWORD ... DISTINCT -_-

INSERT INTO [DMS].[dbo].[Deductions]
                    (
                     CustomerID,
                     DeductionCode,
                     DeductionDescription
                    )
                    SELECT  DISTINCT
                            a.CustomerID,
                            ISNULL(a.AdjustmentReason, 'UNKNOWN'),
                            ISNULL(a.AdjustmentReason, 'UNKNOWN')
                    FROM    @CreditDebitAdjustmentDetail a
                    WHERE   NOT EXISTS ( SELECT 1
                                         FROM   [DMS].[dbo].[Deductions] b
                                         WHERE  a.CustomerID = b.CustomerID
                                                AND CASE a.AdjustmentReason
                                                      WHEN NULL THEN 'UNKNOWN'
                                                      WHEN '' THEN 'UNKNOWN'
                                                    END = b.DeductionCode )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top