“delete from table where 条件" 是删除条件下的所有记录,但是如果只删除条件下的前几条记录,又该怎么处理?
一般处理方法为:
delete table from (select top n * from table where 条件) as tmpTable where table.主键=tmpTable.主键
或
delete from table where table.主键 in (select top n 主键 from table where 条件)
对于上面的SQL可以处理大部分的数据,但是特殊情况,如数据存在相同的记录,再采取这样的方法就有问题。
针对所有情况且处理简单的方法:
1)delete top(n) from table where 条件
2)set rowcount n
delete from table where 条件
set rowcount 0
set rowcount 在指定的行数后停止处理。
posted on
浙公网安备 33010602011771号