代码之家  ›  专栏  ›  技术社区  ›  confusedMind

理解Sql Server合并以进行大容量插入时出现问题

  •  0
  • confusedMind  · 技术社区  · 4 年前

    我生成一个 DataTable 我先去掉所有的行 在C#。

    然后,我将DataTable传递给一个存储过程,以便进行大容量插入,但我随机得到一个错误声明:

    但让我困惑的是,在将数据表发送到存储过程之前,我从数据表中删除了所有重复的行。

    代码:

    public static DataTable RemoveDuplicateRows(DataTable dTable, string colName)
    {
        Hashtable hTable = new Hashtable();
        ArrayList duplicateList = new ArrayList();
    
        //Add list of all the unique item value to hashtable, which stores combination of key, value pair.
        //And add duplicate item value in arraylist.
        foreach (DataRow drow in dTable.Rows)
        {
            if (hTable.Contains(drow[colName]))
                duplicateList.Add(drow);
            else
                hTable.Add(drow[colName], string.Empty);
        }
    
        //Removing a list of duplicate items from datatable.
        foreach (DataRow dRow in duplicateList)
            dTable.Rows.Remove(dRow);
    
        //Datatable which contains unique records will be return as output.
        return dTable;
    }
    

    ALTER PROCEDURE [dbo].[Update_DataFeed_Discoverable]
      @tblCustomers DateFeed_Discoverable READONLY
      AS
      BEGIN
      SET NOCOUNT ON;
    
      MERGE INTO DataFeed c1
      USING @tblCustomers c2
      ON c1.CheckSumProductName=checksum(c2.aw_deep_link)
      WHEN MATCHED THEN
      UPDATE SET 
            c1.merchant_name = c2.merchant_name
            ,c1.aw_deep_link = c2.aw_deep_link
            ,c1.brand_name = c2.brand_name
            ,c1.product_name = c2.product_name
            ,c1.merchant_image_url = c2.merchant_image_url
            ,c1.Price = c2.Price
    
      WHEN NOT MATCHED THEN
      INSERT VALUES(c2.merchant_name, c2.aw_deep_link,
      c2.brand_name, c2.product_name, c2.merchant_image_url,
      c2.Price,1,checksum(c2.aw_deep_link)
    
      );
    END
    

    因此,如果在使用DataTable之前没有插入和删除重复的行,则会导致重复的行。

    感谢您的帮助。

    0 回复  |  直到 4 年前