Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert into table if record does not already exist

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 )
like image 286
JJ. Avatar asked Dec 06 '25 04:12

JJ.


1 Answers

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 )
like image 61
JJ. Avatar answered Dec 07 '25 18:12

JJ.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!