gpt4 book ai didi

SQL Server : How to use Cursor to delete records

转载 作者:行者123 更新时间:2023-12-03 03:09:52 26 4
gpt4 key购买 nike

我们想要删除表中大量过时的数据,但这会锁定表很长一段时间。是否可以使用 Cursor 在 while 循环中删除每个事务一百条记录?

哪里可以引用这个例子?

最佳答案

类似这样的事情:

DECLARE @stillgoing bit;
SET @stillgoing = 1;

WHILE @stillgoing = 1
BEGIN
DELETE TOP (100) YourTableName
WHERE IsObsolete = 1;

IF @@ROWCOUNT = 0
SET @stillgoing = 0;

CHECKPOINT /* Will encourage the log to clear if it's in Simple recovery model */
END

编辑:这仅适用于 SQL 2005 及更高版本。正如我们刚刚了解到的,它是 SQL 2000,代码如下:

DECLARE @stillgoing bit
SET @stillgoing = 1

SET ROWCOUNT 100

WHILE @stillgoing = 1
BEGIN
DELETE YourTableName
WHERE IsObsolete = 1

IF @@ROWCOUNT = 0
SET @stillgoing = 0

CHECKPOINT /* Will encourage the log to clear if it's in Simple recovery model */
END

并且...简单恢复模型意味着日志将在检查点上截断,而不是仅在备份日志时截断。

关于SQL Server : How to use Cursor to delete records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2098817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com