即使经过调查,仍然存在FK冲突

时间:2015-07-03 06:57:15

标签: sql sql-server ssis

4.857万行经过流OK,只有38.000行发送到错误输出。这是由于FK冲突造成的。 ---> “INSERT语句与FOREIGN KEY约束”FK_FactTransactions_DimCustomer“冲突。 冲突发生在数据库“”,表“dbo.DimCustomer”,列'CustomerNr'。“。

我的问题是,经过一些调查后,我无法识别主表dbo.dimcustomer中的任何冲突。

让我们以Mid(在DB中命名为CustomerNr)“60534658”为例,让我们看看。

enter image description here

enter image description here

enter image description here

图片NR 1:这是一些被发送到错误输出以进行分析的行的数据视图草稿。

2:这是它应该插入的表,注意具有相同CustomerNr的行已经存在,因为出于某种奇怪的原因,同一CustomerNr的某些行被插入而其他行没有插入

3:最后。这是实际的主表(Customer表),其中Mid(CustomerNr)引用显然存在!

我在这里遗漏了什么吗?为什么还有冲突? 任何答案的ty!

表格结构:

CREATE TABLE [dbo].[DimCustomer](
    [CustomerNr] [int] NOT NULL,
    [CustomerID] [int] NULL,
    [GeographyKey] [int] NULL,
    [OrgNum] [nvarchar](50) NULL,
    [CustomerName] [nvarchar](255) NULL,
    [Adress] [nvarchar](255) NULL,
    [ZipCode] [nvarchar](255) NULL,
    [MCC_Code] [float] NULL,
 CONSTRAINT [PK_Dim.Customer_1] PRIMARY KEY CLUSTERED 
(
    [CustomerNr] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[DimCustomer]  WITH CHECK ADD  CONSTRAINT [FK_DimCustomer_DimGeography] FOREIGN KEY([GeographyKey])
REFERENCES [dbo].[DimGeography] ([GeographyKey])
GO

ALTER TABLE [dbo].[DimCustomer] CHECK CONSTRAINT [FK_DimCustomer_DimGeography]

GO

CREATE TABLE [dbo].[FactTransactions](
    [TransactionKey] [int] IDENTITY(1,1) NOT NULL,
    [Reportdate] [date] NULL,
    [CustomerNr] [int] NULL,
    [SchemeID] [smallint] NULL,
    [PriceType] [int] NULL,
    [Count] [int] NULL,
    [Amount] [float] NULL,
    [Commission] [float] NULL,
    [InterchangeFee] [float] NULL,
    [Currency] [nvarchar](3) NULL,
    [FeeType] [int] NULL,
 CONSTRAINT [PK_FactTransactions] PRIMARY KEY CLUSTERED 
(
[TransactionKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[FactTransactions]  WITH CHECK ADD  CONSTRAINT [FK_FactTransactions_DimCardScheme] FOREIGN KEY([SchemeID])
REFERENCES [dbo].[DimCardScheme] ([SchemeID])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCardScheme]
GO
ALTER TABLE [dbo].[FactTransactions]  WITH CHECK ADD  CONSTRAINT [FK_FactTransactions_DimCustomer] FOREIGN KEY([CustomerNr])
REFERENCES [dbo].[DimCustomer] ([CustomerNr])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCustomer]
GO
ALTER TABLE [dbo].[FactTransactions]  WITH CHECK ADD  CONSTRAINT [FK_FactTransactions_DimDate] FOREIGN KEY([Reportdate])
REFERENCES [dbo].[DimDate] ([Date])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimDate]
GO
ALTER TABLE [dbo].[FactTransactions]  WITH CHECK ADD  CONSTRAINT [FK_FactTransactions_DimPriceType] FOREIGN KEY([PriceType])
REFERENCES [dbo].[DimPriceType] ([PriceType])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimPriceType]
GO

示例错误输出行:

2015-05-01,60534658,1,1,57,484,5280,3000000000002,78,340000000000003,0,欧元,57.1

1 个答案:

答案 0 :(得分:0)

也许你有一些FactTrancsactions指向一个不存在的客户。

尝试使用此语句检查是否有某些事务指向错误的客户。如果您创建外键,则此语句返回的每一行都将跳出。

SELECT ft.*
FROM dbo.FactTransactions as ft
LEFT JOIN dbo.DimCustomer as dc
        ON ft.CustomerNr = dc.CustomerNr
WHERE dc.CustomerNr IS NULL