[英]Foreign key Constraints - Writing to Error Table - SQL Server 2008
I am new to SQL Server. 我是SQL Server的新手。 I have a Batch process that loads data into my stage tables.
我有一个批处理过程,可将数据加载到我的阶段表中。 I have some foreign Keys on that table.
我在那张桌子上有一些外键。 I want to dump all the foreign key errors encountered while loading into a error table.
我想转储加载到错误表时遇到的所有外键错误。 How do I do that?
我怎么做?
Thanks New Novice 谢谢新手
Use SSIS to load the data. 使用SSIS加载数据。 Records which fail validation can be sent off to a an exception table.
验证失败的记录可以发送到异常表。
One approach would be to load the data into a temporary table which has no FK constraints, remove the bad records (that violate the FK constraints), then move the data from the temp table into the stage table. 一种方法是将数据加载到没有FK约束的临时表中,删除坏记录(违反FK约束),然后将数据从临时表移到阶段表中。 If you have lots of FKs on your table this will probably be a bit tedious, so you would probably want to automate the process.
如果您的桌子上有很多FK,这可能会有些乏味,因此您可能希望自动化该过程。
Here's some pseudo-code to show what I mean... 这是一些伪代码来说明我的意思...
-- First put the raw data into MyTempTable
-- Find the records that are "bad" -- you can SELECT INTO a "bad records" table
-- for later inspection if you want...
SELECT *
INTO #BadRecords
FROM MyTempTable
WHERE ForeignKeyIDColumn NOT IN
(
SELECT ID FROM ForeignKeyTable
)
-- Remove the bad records now
DELETE
FROM MyTempTable
WHERE ForeignKeyIDColumn NOT IN
(
SELECT ID FROM ForeignKeyTable
)
-- Now the data is "clean" (won't violate the FK) so you can insert it
-- from MyTempTable into the stage table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.