试试这样的-
避免
任何
一排排痛苦的排
类似光标或while循环的处理:
-- creates a temporary "inline" view of the data you're interested in and
-- assigns each row a consecutive number
;WITH DataToUpdate AS
(
SELECT
DocumentHeaderId,
DocumentNumber,
NewDocNum = ROW_NUMBER() OVER(ORDER BY [Date])
FROM
Accounting.DocumentHeader
WHERE
FinancialPeriodFK = @FinancialPeriodFK
AND Date >= @FromDate
AND Date <= @ToDate
)
-- update the base table to make use of that new document number
UPDATE dh
SET dh.DocumentNumber = dtu.NewDocNum
FROM Accounting.DocumentHeader dh
INNER JOIN DataToUpdate dtu ON dh.DocumentHeaderId = dtu.DocumentHeaderId
那应该
明显地
加快处理时间!