Sql SSIS增量数据加载错误
我正在尝试执行从暂存表(cust_reg_dim_stg)到仓库表(dim_cust_reg)的增量插入。这就是我正在使用的查询Sql SSIS增量数据加载错误,sql,sql-server,ssis,sql-server-2012,Sql,Sql Server,Ssis,Sql Server 2012,我正在尝试执行从暂存表(cust_reg_dim_stg)到仓库表(dim_cust_reg)的增量插入。这就是我正在使用的查询 insert into dim_cust_reg WITH(TABLOCK) ( channel_id ,cust_reg_id ,cust_id ,status ,date_created ,date_activated ,date_archived ,custodian_id ,reg_ty
insert into dim_cust_reg WITH(TABLOCK)
(
channel_id
,cust_reg_id
,cust_id
,status
,date_created
,date_activated
,date_archived
,custodian_id
,reg_type_id
,reg_flags
,acc_name
,acc_number
,sr_id
,sr_type
,as_of_date
,ins_timestamp
)
select channel_id
,cust_reg_id
,cust_id
,status
,date_created
,date_activated
,date_archived
,reg_type_id
,reg_flags
,acc_name
,acc_number
,sr_id
,sr_type
,as_of_date
,getdate() ins_timestamp
from umpdwstg..cust_reg_dim_stg stg with(nolock)
join lookup_channel ch with(nolock) on stg.channel_name = ch.channel_name
where not exists
(select * from dim_cust_reg dest
where dest.cust_reg_id=stg.cust_reg_id
and dest.sr_id=stg.sr_id
and dest.channel_id=ch.channel_id )
此处,通道id不在暂存表中,使用通道查找表(查找通道)获取。运行此查询时,我遇到以下错误
Violation of PRIMARY KEY constraint 'PK__dim_cust__4A293521A789A5FA'.
Cannot insert duplicate key in object 'dbo.dim_cust_reg'.
这个查询有什么问题?channel_id、sr_id和cust_reg_id构成唯一的密钥组合。似乎没有数据错误 如果表-custr\u regr\u dim\u stg中已经有重复的条目,则SELECT查询将生成这两个记录,并尝试将其插入dim\u cust\u reg表中。因此,请在SELECT语句中使用DISTINCT。有两个方面需要进行故障排除:- 在以下代码中:
join lookup_channel ch with(nolock) on stg.channel_name = ch.channel_name
与目标维度中的记录相比,暂存表中的传入通道名称可能具有不同的通道名称
或
这可能是因为NOT EXISTS条件中存在此联接条件:
and dest.sr_id=stg.sr_id
and dest.channel_id=ch.channel_id
在这里,当您将暂存数据与目的地中的数据进行比较时,输入通道_id可能也不同。因此,建议忽略通道id一次,并尝试进行故障排除。一旦将此数据加载到目标中,您就可以得到错误是否是由于通道id引起的确切原因
故障排除愉快 主键,即通道id具有重复值,可能来自查找通道。请检查select query from insert语句