【发布时间】:2018-11-03 05:04:31
【问题描述】:
我正在尝试执行从临时表 (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_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 )
这里的 channel_id 不在临时表中,而是使用频道查找表 (lookup_channel) 获取的。在运行此查询时,我收到以下错误。
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 形成唯一的组合键。似乎没有数据错误。
【问题讨论】:
-
主键即channel_id有重复值,可能来自查找通道。检查插入语句中的选择查询
标签: sql sql-server ssis sql-server-2012