sql - 如何多次使用存储过程从 SQL Server 中的大表中搜索字符串
问题描述
我想使用存储过程将 200k+ 产品从 Excel 文件存储到 SQL Server 中。我有两张桌子(products
,Inventory
)
首先我检查产品的条形码,如果产品存在,则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
解决方案
推荐阅读
- c# - 如何使用 PowerShell 禁用 Windows 窗体标题栏上的右键单击上下文菜单
- javascript - sqlite3字符串中不允许使用哪些字符而不添加其他内容
- python - 如何提高 Python (Numpy) 中数组切片和操作的效率?
- javascript - 创建会话(passport、node js、express-session)后如何从同一项目中的另一个文件访问会话变量
- mongodb - 基于mongodb聚合中的另一个属性在嵌套数组中查找对象
- c# - MySql 异常使用 DateTimeOffset.UtcNow 时无法序列化日期/时间值
- sql - 递归查找 PostgreSQL 10 层次结构
- python - 在 python 中使用 matpltlib 中的动画工具绘制移动(动画)椭圆体
- sql - 查找相交日期
- flutter - 我如何使用 bloc 在颤动中切换主题?