我在SQL Server 2012上工作
我有临时表从Excel中获取数据,并且基于Excel中存在的数据我在循环内的表中插入
临时表始终有大量数据,可能至少为5000或10000或15000或更多
我需要从临时表中插入的每个迭代增加5000行
所以我需要像这样的速度和内存的最佳解决方案
如果有什么不正确的逻辑请告诉我
我的查询如下:
我有临时表从Excel中获取数据,并且基于Excel中存在的数据我在循环内的表中插入
临时表始终有大量数据,可能至少为5000或10000或15000或更多
我需要从临时表中插入的每个迭代增加5000行
所以我需要像这样的速度和内存的最佳解决方案
如果有什么不正确的逻辑请告诉我
我的查询如下:
SQL:
create table #Temp(
DocumentPartID int identity(1,1),
CompanyName VARCHAR(4000),
[AffectedProduct] NVARCHAR(4000),
[ReplacementPart] VARCHAR(4000) ,
[ReplacementCompany] VARCHAR(4000) ,
[Category] VARCHAR(4000) ,
DocumentID int null,
CompanyID VARCHAR(4000) null,
PartID int null,
ReplacementPartID int null,
CategoryID int null,
[Status] VARCHAR(4000) null ,
)
insert into #Temp
(
CompanyName ,
[AffectedProduct],
[ReplacementPart],
[ReplacementCompany],
[Category]
)
values
('Nokia','RF1550','RF1550','HTS','HTS'),
('IPHONE','TF1545','TF1545','Corning Incorporated','HTS2')
DECLARE @MaxValue int = ( select Max(DocumentPartID) from #Temp)
DECLARE @Currentindex int =0
DECLARE @Rows [dbo].[Type_ValidationInPut];
while @Currentindex < @MaxValue
begin
DELETE @Rows
INSERT INTO @Rows
(
RowNumber ,
GivenPartNumber ,
GivenManufacturer
)
select TOP 5000 DocumentPartID , isnull(AffectedProduct,''), isnull(CompanyName,'') FROM #Temp where
(CategoryID = 517884 or CategoryID = 1110481) and (DocumentPartID > @Currentindex) and [Status] is null
INSERT INTO @Rows
(
RowNumber ,
GivenPartNumber ,
GivenManufacturer
)
select TOP 5000 DocumentPartID, isnull(substring(ReplacementPart,0,70),''), isnull(ReplacementCompany,'') FROM #Temp where
(DocumentPartID > @Currentindex) and [Status] is null and ReplacementPart is not null
DECLARE @NewID nVARCHAR(4000) =newID()
insert into [ls30].[validation].[dbo].PartsData (BatchID,RowNumber,GivenPartNumber,givenmanufacturer)
SELECT @NewID ,0,GivenPartNumber,GivenManufacturer from @Rows
set @Currentindex = @Currentindex +5000
DELETE @Rows
end
由主持人最后编辑: