Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 插入到&;唯一约束SQL Server 2008_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server 插入到&;唯一约束SQL Server 2008

Sql server 插入到&;唯一约束SQL Server 2008,sql-server,sql-server-2008,Sql Server,Sql Server 2008,运行Insert语句时出现以下问题: 我得到了两个表:一个临时表,其中包含通过SSIS导入的客户订单信息(订单csv导入)和一个主客户表(客户) 我创建的作业中的任务之一是创建客户(如果尚未存在): insert into dbo.[Customer] (FirstName, LastName, PostalAddress1, PostalAddress2, State_Prov, City, ZIPPostalCode, Country,

运行Insert语句时出现以下问题:

我得到了两个表:一个临时表,其中包含通过SSIS导入的客户订单信息(订单csv导入)和一个主客户表(客户)

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

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
表上获得了一个唯一的键约束,该约束包含名字、姓氏、出生日期、月份和年份

味精2627,第14级,状态1,第3行
违反唯一密钥约束“约束\唯一\客户\存在”。无法在对象“dbo.Customer”中插入重复的密钥

我研究了它,但我能找到的所有解决方案都是基于在目标表中识别insert上的重复项

在我的例子中,我需要删除源表中两个条目中的一个


有什么想法吗

使用
窗口功能
删除重复项

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

Row_number() OVER(partition BY billing_first_name, billing_last_name, 
                              bday, 
                              bmonth ,byear 
                                ORDER BY bmonth, byear ) Rn
将select查询设置为sub select,在外部查询中,您可以通过选中
来过滤重复项,其中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 

嗯。。。字面上只是复制和粘贴。。。它给了我一个提示:Msg 8155,16级,状态2,第64行“A”的第15列未指定列名。@user2301990您是否盲目检查了完整的代码?请勿评论我已使用窗口功能删除子选择中的重复项,错误是因为我错过了子选择中的
check\u by
alias您应该能够修复这些错误。更新了答案抱歉,正在忙于其他事情-一旦我实际使用了有效的列名,它就可以正常工作-到目前为止,我只有一个“0”作为check_in_by的值。。。所以我把它改成了一个列名,它工作得很好。感谢并抱歉未经检查就发表评论;)