ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 276.7k

How to replace while loop with another best practice ?

Feb 12 2020 7:38 PM
I work on SQL server 2012
 
I have temp table get data from excel and based on data exist on excel i insert on table inside loop
 
temp table always have big amount of data may be at least 5000 or 10000 or 15000 or more
 
I need every iteration increased by 5000 rows insert from temp table
 
so that i need best solutions for that according to speed and memory like that
 
and if there are any thing not correct as logic please tell me
 
my Query as below :
 
  1. --create table [dbo].PartsData  
  2. Create Table [dbo].PartsData  
  3. (  
  4.     BatchID nvarchar(50) primary key,  
  5.     RowNumber int,  
  6.     GivenPartNumber nvarchar(50),  
  7.     GivenManufacturer nvarchar(100)  
  8.   
  9. )  
  10.   
  11. --select * from [dbo].PartsData  
  12. Create Table [dbo].[Type_ValidationInPut]  
  13. (  
  14.     RowNumber int,  
  15.     GivenPartNumber nvarchar(50),  
  16.     GivenManufacturer nvarchar(100)  
  17.   
  18. )  
  19. --drop table #Temp  
  20. create table #Temp(  
  21.     DocumentPartID int identity(1,1),  
  22.     CompanyName VARCHAR(4000),  
  23.     [AffectedProduct] NVARCHAR(4000),  
  24.     [ReplacementPart] VARCHAR(4000) ,   
  25.     [ReplacementCompany] VARCHAR(4000) ,  
  26.     [Category] VARCHAR(4000) ,  
  27.   
  28.       
  29.     DocumentID int null,    
  30.     CompanyID VARCHAR(4000) null,  
  31.     PartID int null,  
  32.     ReplacementPartID int null,  
  33.     CategoryID  int null,  
  34.     [Status]  VARCHAR(4000) null ,  
  35.   
  36.   
  37. )  
  38.   
  39.   
  40.   
  41. insert into #Temp  
  42. (  
  43. CompanyName ,  
  44. [AffectedProduct],  
  45. [ReplacementPart],  
  46. [ReplacementCompany],  
  47. [Category]  
  48. )    
  49. values  
  50. ('Nokia','RF1550','RF1550','HTS','HTS'),  
  51. ('IPHONE','TF1545','TF1545','Corning Incorporated','HTS2')  
  52.   
  53.   
  54.   
  55. DECLARE @MaxValue int = ( select Max(DocumentPartID) from #Temp)  
  56. DECLARE @Currentindex int =0  
  57. --DECLARE @Rows  [dbo].[Type_ValidationInPut];  
  58. DECLARE @Rows  [dbo].[Type_ValidationInPut];      
  59.            while @Currentindex < @MaxValue  
  60.             begin   
  61.               
  62.             
  63.               DELETE @Rows  
  64.               INSERT  INTO @Rows  
  65.                         (  
  66.                         RowNumber ,  
  67.                  GivenPartNumber ,  
  68.                   GivenManufacturer         
  69.                 )   
  70.              
  71.              
  72.             select TOP 5000 DocumentPartID , isnull(AffectedProduct,''), isnull(CompanyName,''FROM #Temp where   
  73.               
  74.             (CategoryID = 517884 or CategoryID = 1110481)  and (DocumentPartID > @Currentindex) and [Status] is null   
  75.   
  76.                 INSERT  INTO @Rows  
  77.                         (  
  78.                  RowNumber ,  
  79.                  GivenPartNumber ,  
  80.                   GivenManufacturer         
  81.                 )   
  82.                
  83.           
  84.             select TOP 5000 DocumentPartID, isnull(substring(ReplacementPart,0,70),''), isnull(ReplacementCompany,''FROM #Temp where     
  85.             (DocumentPartID > @Currentindex) and  [Status] is null and ReplacementPart is not null  
  86.           
  87.   
  88.             DECLARE @NewID nVARCHAR(4000) =newID()  
  89.             insert into [dbo].PartsData (BatchID,RowNumber,GivenPartNumber,givenmanufacturer)   
  90.             SELECT  @NewID ,0,GivenPartNumber,GivenManufacturer from  @Rows   
  91.               
  92.   
  93.               
  94.             set @Currentindex = @Currentindex +5000  
  95.             DELETE @Rows  
  96.              end  
 

Answers (9)