[英]C# SQL How to select and process N records, and then update them?
I retrieve some records by running this query: 我通过运行此查询来检索一些记录:
SELECT * FROM [MyTable] order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
Where N is a number that starts from 0 and increment by 500. There are multiple instances of this application and each instance gets 500 records. 其中N是一个从0开始并递增500的数字。此应用程序有多个实例,每个实例获取500条记录。
Now the question is, how do I update the retrieved records? 现在的问题是,如何更新检索到的记录? The records don't have primary key. 记录没有主键。 I tried something like this but the syntax isn't right: 我尝试了类似的方法,但是语法不正确:
UPDATE [MyTable] SET [status] = 1 order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS
Note: I can't use WHERE status = 0 because I want one instance to only deal with the records it retrieved. 注意:我不能使用WHERE status = 0,因为我希望一个实例仅处理它检索到的记录。
Any idea? 任何想法?
You could try something like this: 您可以尝试这样的事情:
UPDATE x
SET [status] = 1
FROM (
SELECT * FROM [MyTable] order by [Date] OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
) x
UPDATE x
SET x.[status] = 1
OUTPUT deleted.*
FROM
(
SELECT *
FROM [MyTable]
WHERE
[status] = 0
ORDER BY [Date]
OFFSET N ROWS FETCH NEXT 500 ROWS ONLY
) x;
Be careful though, your "fetch next" is preventing any rows from being returned if there are not at least 500 to return. 但是请小心,如果没有至少500行要返回,则“下一步获取”将阻止返回任何行。 Maybe that's what you want. 也许这就是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.