插入到&中的重复唯一约束SQL Server 2008

时间:2014-12-21 11:18:52

标签: sql-server sql-server-2008

运行插入声明时遇到以下问题:

我有2个表:一个临时表,其中包含通过SSIS(order-csv-import)和主Customer表(Customer)导入的客户订单信息。

我创建的工作中的任务之一是创建客户(如果尚未存在):

insert into dbo.[Customer] (FirstName, LastName, PostalAddress1, PostalAddress2,
                            State_Prov, City, ZIPPostalCode, Country,
                            Email, BYear, BMonth, BDay, Gender,
                            CheckInDate, Check_In_by,  Comments)
   Select distinct 
       billing_first_name,
       billing_last_name,
       billing_address_1,
       billing_address_2,
       PADI_State,
       billing_city,
       bil ling_postcode,
       PADI_Country,
       billing_email,
       BYear,
       BMonth,
       BDay,
       Gender,
       arrival_date,
       '0',
       customer_note
   from 
       dbo.[orders-csv-import] OCI
   where 
       not exists (select 1 
                   from dbo.Customer TCI 
                   where TCI.LastName = OCI.billing_last_name
                     and TCI.FirstName = OCI.billing_first_name
                     and TCI.BDay = OCI.BDay
                     and TCI.BMonth = OCI.BMonth
                     and TCI.BYear = OCI.BYear)

这非常有效,Customer表中的现有客户将被忽略。当我让同一个客户连续两次预订时,问题就出现了。现在我的临时源表两次获得同一个客户,并且因为我在Customer表上获得了一个名字,姓氏,出生日,月份和年份组合的唯一键约束,所以它会引发错误。< / p>

  

Ms 2627,Level 14,State 1,Line 3
  违反UNIQUE KEY约束'constraint_Unique_CUSTOMER_EXISTS'。无法在对象'dbo.Customer'中插入重复键。

我调查了一下,但我能找到的所有解决方案都是基于在目标表中插入重复项。

在我的情况下,我需要删除源表中的两个条目之一。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用Window Function删除重复项

为了避免基于您创建的UQ的重复,您需要使用窗口函数

Row_number() OVER(partition BY billing_first_name, billing_last_name, 
                              bday, 
                              bmonth ,byear 
                                ORDER BY bmonth, byear ) Rn

将选择查询设为子选择,在外部查询中,您可以通过选中where Rn=1来过滤掉重复项。插入应该像

INSERT INTO dbo.[customer] 
            (firstname, 
             lastname, 
             postaladdress1, 
             postaladdress2, 
             state_prov, 
             city, 
             zippostalcode, 
             country, 
             email, 
             byear, 
             bmonth, 
             bday, 
             gender, 
             checkindate, 
             check_in_by, 
             comments) 
SELECT DISTINCT billing_first_name, 
                billing_last_name, 
                billing_address_1, 
                billing_address_2, 
                padi_state, 
                billing_city, 
                billing_postcode, 
                padi_country, 
                billing_email, 
                byear, 
                bmonth, 
                bday, 
                gender, 
                arrival_date, 
                check_in_by, 
                customer_note 
FROM   (SELECT billing_first_name, 
                        billing_last_name, 
                        billing_address_1, 
                        billing_address_2, 
                        padi_state, 
                        billing_city, 
                        billing_postcode, 
                        padi_country, 
                        billing_email, 
                        byear, 
                        bmonth, 
                        bday, 
                        gender, 
                        arrival_date, 
                        '0' check_in_by, 
                        customer_note, Row_number() 
                          OVER( 
                            partition BY billing_first_name, billing_last_name, 
                          bday, 
                          bmonth ,byear 
                            ORDER BY bmonth, byear ) Rn 
        FROM   dbo.[orders-csv-import] OCI 
        WHERE  NOT EXISTS (SELECT 1 
                           FROM   dbo.customer TCI 
                           WHERE  TCI.lastname = OCI.billing_last_name 
                                  AND TCI.firstname = OCI.billing_first_name 
                                  AND TCI.bday = OCI.bday 
                                  AND TCI.bmonth = OCI.bmonth 
                                  AND TCI.byear = OCI.byear)) A 
WHERE  rn = 1