如何在不违反主要约束的情况下插入随机记录

时间:2014-08-25 19:33:36

标签: sql sql-server insert duplicates distinct

如果有误导性,请原谅我的头衔,但我有一张tb_party PRIMARY KEY on party_key的表{/ 1>

我有以下查询 -

 insert TMS..tb_party
        (
            [party_key]
            ,[party_first_name]
            ,[tax_id]
            ,[party_type_cd]
            ,[citizenship_country_cd]
            ,[domicile_country_cd]
            ,[party_num]
            ,[batch_dt]
        )


    select distinct
            [party_key]
            ,[party_first_name]
            ,[tax_id]
            ,[party_type_cd]
            ,[cit]
            ,[dom]
            ,[party_num]
            ,[batch_dt]

        from 
        (select distinct
                 sca.[party_key]
                ,sca.[party_first_name]
                ,sca.[tax_id]
                ,pt.party_type_cd
                ,tc_cit.COUNTRY_ID as cit
                ,tc_dom.COUNTRY_ID as dom
                ,sca.[party_num]
                ,getdate() as batch_dt
                ,dense_rank() over(partition by sca.[party_key] order by sca.party_key) as rnk
            from Iteration_3.dbo.staging_cust_acct  sca     (nolock)
            join Iteration_3..STG_PARTY_UPLOAD (nolock) stg_party
                on sca.party_key = stg_party.PARTY_KEY
            left join Iteration_3..STG_COUNTRY_ISO              tc_dom  (nolock)
                on sca.[domicile_country] = tc_dom.COUNTRY_NAME
            left join Iteration_3..STG_COUNTRY_ISO              tc_cit  (nolock)
                on sca.[citizenship_country] = tc_cit.COUNTRY_NAME
            left join TMS..tb_party_type                pt      (nolock)
                on sca.[party_type] = pt.party_type_desc
            WHERE  
            SCA.party_type IS NOT NULL
            ) x 
        where rnk = 1

插入失败是因为它试图插入重复的聚会密钥,并且由于不同的是在所有列上,所以它正在拾取重复的party_keys。

我想要什么 - 我想选择所有不同的party_keys并在tb_party中插入1行。其他行可以忽略。这可能吗?

2 个答案:

答案 0 :(得分:0)

这就是row_number派上用场的地方。

select distinct
        [party_key]
        ,[party_first_name]
        ,[tax_id]
        ,[party_type_cd]
        ,[cit]
        ,[dom]
        ,[party_num]
        ,[batch_dt]

    from 
    (


        select distinct
             sca.[party_key]
            ,sca.[party_first_name]
            ,sca.[tax_id]
            ,pt.party_type_cd
            ,tc_cit.COUNTRY_ID as cit
            ,tc_dom.COUNTRY_ID as dom
            ,sca.[party_num]
            ,getdate() as batch_dt
            --,dense_rank() over(partition by sca.[party_key] order by sca.party_key,sca.[tax_id],sca.[party_num],sca.[party_first_name]*/) as rnk
            ,row_number() over (partition by sca.party_key order by sca.party_key) rn
        from Iteration_3.dbo.staging_cust_acct  sca     (nolock)
        join Iteration_3..STG_PARTY_UPLOAD (nolock) stg_party
            on sca.party_key = stg_party.PARTY_KEY
        left join Iteration_3..STG_COUNTRY_ISO              tc_dom  (nolock)
            on sca.[domicile_country] = tc_dom.COUNTRY_NAME
        left join Iteration_3..STG_COUNTRY_ISO              tc_cit  (nolock)
            on sca.[citizenship_country] = tc_cit.COUNTRY_NAME
        left join TMS..tb_party_type                pt      (nolock)
            on sca.[party_type] = pt.party_type_desc
        WHERE --SCA.update_source  = 'ECM' AND SCA.update_dt = @ECM_MAX_DATE
        --and 
        SCA.party_type IS NOT NULL
        ) x 
    where rn = 1 

答案 1 :(得分:0)

尝试在唯一列上合并到目标表中。如果不匹配,请插入。匹配更新时如下: ... set target。[column] = coalesce(source。[column],target。[column]) ... 这样,当您拥有源值时,您将更新所有目标列,否则它们将保持不变。)