ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.1k

How to write dynamic statement with way do it run quickly?

Jun 24 2020 9:42 PM

I work on SQL server 2012 I face Issue I implement dynamic SQL it very slow

so I need to enhance statement below to work quickly
  1. DECLARE @SQL NVARCHAR(MAX)    
  2.     select @SQL =CONCAT('      
  3.  SELECT *  Into #NewTable    
  4. FROM #SplitNumberAndUnitsFinal    
  5. PIVOT(max(Value) FOR DKFeatureName IN ('+@Columns+')) AS PVTTable         
  6. ',    
  7. N'  Select ''Variant ID'' as ''[Variant ID]'',''PART_ID'' as ''PART_ID'' ,''Vendor ID'' as ''Vendor ID'',''Manufacturer'' as ''Manufacturer'',''Digi-Key Part No.'' as ''Digi-Key Part No.'' ,''Mfr Part No.'' as ''Mfr Part No.'' ,''Description'' as ''Description'',''Category'' as ''Category'',''Family'' as ''Family'',''Obsolete'' as ''Obsolete'' ,''Non-Stock'' as ''Non-Stock'' , ''Part Status'' as ''Part Status'',''PartNumber'' as ''Z2 Part number'' , ''CompanyName'' as ''Z2 Manufacturer'' ,   ' +@Header + '     
  8. union all    
  9. select [Variant Number],PART_ID,[Vendor ID],Manufacturer,[Digi-Key Part No.] ,[Mfr Part No.],[Description],Category,Family ,Obsolete ,[Non-Stock] , [Part_Status], PartNumber , CompanyName  ,   ' +@Columns + '  from  #NewTable  
 

so please any advice for code above to enhance it to work quickly ?

so are there are any thing wrong you see on code above ?