Я улучшил производительность цикла while, вы можете использовать этот запрос, чтобы зафиксировать ошибку.
CREATE TABLE #Temp_Error
(
[Row Number] NUMERIC(18,0),
[HTMLBody] NVARCHAR(MAX)
)
CREATE TABLE #Temp_Error_Log
(
[Row Number] NUMERIC(18,0),
[HTMLBody] NVARCHAR(MAX)
)
CREATE TABLE #Temp_Error_Desc
(
[Row Number] NUMERIC(18,0),
Test1 NVARCHAR(2000),
Test2 NVARCHAR(2000),
Test3 NVARCHAR(2000),
)
INSERT INTO #Temp_Error
(
[Row Number],[HTMLBody]
)
SELECT [Row Number],[HTMLBody]
FROM [ErrorDB].[dbo].[Error]
DECLARE @BATCH INT=100,@N_TOTAL INT
SELECT @BATCH=Count(1) FROM #TEMP_ERROR
SELECT @N_TOTAL=@BATCH
WHILE @BATCH>0
BEGIN
BEGIN TRY
;WITH CTE AS
(
SELECT TOP (@BATCH)
[Row Number],
SUBSTRING([HTMLBody], patindex('%factuur%', [HTMLBody]) + 8, patindex('%van leverancier%', [HTMLBody]) - patindex('%factuur%', [HTMLBody]) - 9) AS Test1,
SUBSTRING([HTMLBody], patindex('%leverancier%', [HTMLBody]) + 12, patindex('% - %', [HTMLBody]) - patindex('%leverancier%', [HTMLBody]) - 12) AS Test2,
SUBSTRING([HTMLBody], patindex('% - %', [HTMLBody]) + 3, patindex('%kon niet%', [HTMLBody]) - patindex('% - %', [HTMLBody]) - 3) AS Test3
FROM #Temp_Error
)
DELETE b
OUTPUT DELETED.*
INTO #Temp_Error_Desc ([Row Number],Test1,Test2,Test3)
FROM CTE b
END TRY
BEGIN CATCH
IF @BATCH<>@N_TOTAL
BEGIN
;WITH CTE AS
(
SELECT TOP (@BATCH) *
FROM #Temp_Error
)
DELETE b
OUTPUT DELETED.*
INTO #Temp_Error_Log ([Row Number],[HTMLBody])
FROM CTE b
END
ELSE
BEGIN
SET @BATCH=IIF(@BATCH<10,1,@BATCH/10)
END
END CATCH
IF NOT EXISTS (SELECT TOP 1 1 FROM #Temp_Error)
BEGIN
INSERT INTO #Temp_Error
(
[Row Number],[HTMLBody]
)
SELECT [Row Number],[HTMLBody]
FROM #Temp_Error_Log
TRUNCATE TABLE #Temp_Error_Log
IF @BATCH=1
BEGIN
SET @BATCH=0
END
ELSE
SET @BATCH=IIF(@BATCH<10,1,@BATCH/10)
END
END
SELECT * FROM #Temp_Error
SELECT * FROM #Temp_Error_Desc
DROP TABLE #Temp_Error
DROP TABLE #Temp_Error_Log
DROP TABLE #Temp_Error_Desc