我正在尝试使用有序查询中的x行数更新sql表,其中x是100000的余额 - 之前的结果。我使用ROWCOUNT限制结果数量,因为x是一个参数,但不认为这有效。
任何人都可以建议没有游标的修复或替代方案吗?
DECLARE @Top int
SET @Top = 100000 - @CountRecords
SET ROWCOUNT @Top
UPDATE
UpdateTable
SET
UpdateField = 'SomeValue'
WHERE
Id IN
(
SELECT
TOP 100% Id
FROM
SelectTable
WHERE
(SelectField1 IS NULL) AND
(SelectFieldDate IS NOT NULL)
ORDER BY
SelectFieldDate
)
SET ROWCOUNT 0
答案 0 :(得分:2)
SET ROWCOUNT也适用于中间结果,因此可能会误导
如果您加载了临时表,则可以通过将更新分为两个步骤来绕过这个步骤
DECLARE @Top int
SET @Top = 100000 - @CountRecords
SET ROWCOUNT @Top
SELECT Id
INTO #foo
FROM
SelectTable
WHERE
(SelectField1 IS NULL) AND
(SelectFieldDate IS NOT NULL)
ORDER BY
SelectFieldDate
SET ROWCOUNT 0
UPDATE
UpdateTable
SET
UpdateField = 'SomeValue'
WHERE
Id IN
(
SELECT id FROM #foo
)