I had to see what `lakh 'is :)
As noted in other comments, this is not the best approach to DML; consider refactoring. How can you combine your dynamic SQL in packages, for example:
DECLARE @sSQL nvarchar(max) SET @sSQL = 'BEGIN TRAN; ' SELECT @sSQL = @sSQL + COLUMN_WITH_INSERT_STATEMENT + '; ' FROM TABLE WHERE [limit number of rows] SET @sSQL = @sSQL + 'COMMIT TRAN ' EXEC(@sSQL)
Thus, you can combine a controlled number of INSERT statements in one transaction. You can control the number of inserts using the WHERE statement (for example, WHERE ID BETWEEN 1 and 100 to execute 100 INSERT at a time). You can execute a cycle through this condition (yes, a cycle, but it will not cycle through individual lines, but instead of conditions, instead of 1 - 100, 101 - 200, 201 - 300, etc.).
source share