首页 > 解决方案 > 如何多次使用存储过程从 SQL Server 中的大表中搜索字符串

问题描述

我想使用存储过程将 200k+ 产品从 Excel 文件存储到 SQL Server 中。我有两张桌子(productsInventory

首先我检查产品的条形码,如果产品存在,则ProductId根据提供的条形码获取,并ProductId入库。

使用库存详细信息,但如果没有找到符合条形码的产品,则创建产品,然后productId使用 new 保存库存productId

查询是 100% 工作,但需要太多时间来保存所有数据。

这是我的代码:

CREATE PROCEDURE [dbo].[AddProductsWithCat_Sp]
    @Category_Id int = 0,
    @ProductId int = 0,
    @ProductInvertory_Id int = 0,
    @Category_Name varchar(3000),
    @ProductName varchar(3000),
    @Brand_Name varchar(2000),
    @Barcode varchar(3000),
    @Description varchar(4000),
    @ProductInventory_UnitPrice decimal,
    @ProductInventory_Status bit,
    @Product_TotalPrice decimal,
    @ProductInventory_SellingPrice decimal,
    @ProductInventory_Qty int,
    @Brand_Name_Urdu varchar(3000),
    @Description_Urdu varchar(4000),
    @ProductName_Urdu varchar(3500),
    @Category_Name_Urdu varchar(2000)
AS 
BEGIN
    DECLARE @CheckCategoryName varchar(200);
    DECLARE @CheckBarcode varchar(500);

    SET @CheckBarcode = (SELECT pp.ProductId FROM Products pp WHERE pp.Barcode LIKE '%@Barcode%');
    SET @CheckCategoryName = (SELECT c.Category_Id FROM Categories c WHERE c.Category_Name = @Category_Name);

    IF (@CheckCategoryName IS NULL)
    BEGIN
        INSERT INTO Categories (Category_Name, Category_Name_Urdu, Status)
        VALUES (@Category_Name, @Category_Name_Urdu, 1);

        SET @CheckCategoryName = (SELECT MAX(cc.Category_Id) FROM Categories cc);
    END;

IF (@CheckBarcode is Null)
Begin 
insert into Products(ProductName,Barcode,Description,Status,DateTime,Brand_Name,Category_Id,ProductImage,Brand_Name_Urdu,Description_Urdu,ProductName_Urdu)
values(@ProductName,@Barcode,@Description,1,GETDATE(),@Brand_Name,@CheckCategoryName,'no Image',@Brand_Name_Urdu,@Description_Urdu,@ProductName_Urdu);

set @CheckBarcode=(select MAX(pp.ProductId) from Products pp);
END;
insert into ProductInventories(ProductId,ProductInventory_UnitPrice,ProductInventory_SellingPrice,ProductInventory_Qty,ProductInventory_Status,DateTime,Product_TotalPrice)
Values(@CheckBarcode,@ProductInventory_UnitPrice,@ProductInventory_SellingPrice,@ProductInventory_Qty,1,GETDATE(),@Product_TotalPrice)

  END

标签: sqlasp.net-corestored-proceduressql-server-2014

解决方案


推荐阅读