我们定期需要导入如下所示的CSV:
Name,SpecID,TestResult1,TestResult2,TestResult3
Alex,ASD123,3.23,452.2,232
Craig,DFG444,453.56,345.3,23
数据以这种方式存储:
SPECIMENTABLE (name,specid,SPECIMENTABLEID)
Alex,ASD123,1
Craig,DFG444,2
和
RESULTTABLE (testresult,result,SPECIMENTABLEID)
TestResult1,3.23,1
TestResult2,452.2,1
TestResult3,232,1
TestResult1, 453.56,2
etc
我像这样转储数据:
public void DumpQuickLabDump()
{
// T-SQL Connection
string connection = "Data Source=gaia;Initial Catalog=SalesDWH;Integrated Security=True";
// Get the data into the DataTable
//dtData = GetData(...);
// Create an object of SqlBulkCopy
SqlBulkCopy objSBC = new SqlBulkCopy(connection);
// Specify the destination table
objSBC.BulkCopyTimeout = 0;
objSBC.BatchSize = 10000;
objSBC.DestinationTableName = "SpecimenTable";
// Write the data to the SQL Server
objSBC.WriteToServer(QuickLabDump);
}
public void DumpTestResults()
{
// T-SQL Connection
string connection = "Data Source=gaia;Initial Catalog=SalesDWH;Integrated Security=True";
// Get the data into the DataTable
//dtData = GetData(...);
// Create an object of SqlBulkCopy
SqlBulkCopy objSBC = new SqlBulkCopy(connection);
// Specify the destination table
objSBC.BulkCopyTimeout = 0;
objSBC.BatchSize = 10000;
objSBC.DestinationTableName = "ResultTable";
// Write the data to the SQL Server
objSBC.WriteToServer(TestResults);
}
有时客户会向我提交CSV以便上载,然后几天后,他们将导入另一个CSV,但其中包含一定百分比的SAME RECORDS。
我如何避免重复数据? (请记住,两个表是从一个CSV文件填充到数据库中的)
解决方案可以是.NET或sql。
非常感谢
最佳答案
您不能直接使用SqlBulkCopy
做您想做的事情。但是,您可以将行批量复制到工作表中,然后使用MERGE
语句进行更新或插入。
但是,这确实需要您的源信息有足够的信息来唯一地标识每一行。
例如,假设SpecimenTable
是objSBC.DestinationTableName
而不是StagingSpecimenTable
。 StagingSpecimenTable是SpecimenTable结构的副本。然后,在大容量复制之后,您可以使用SqlCommand执行此语句
MERGE SpecimenTable AS target
USING (SELECT name,specid FROM StagingSpecimenTable)
AS source (StagingSpecimenTable)
ON ( target.specid = source.specid )
WHEN MATCHED
THEN UPDATE SET target.mame= source.name
WHEN NOT MATCHED
THEN INSERT (name, specid )
VALUES (source.name, source.specid )
然后,您必须删除或截断StagingSpecimenTable以及ResultTable的类似操作