如何使用 Sql Server 2008 从表中删除前 1000 行? [英] How to delete the top 1000 rows from a table using Sql Server 2008?

查看:29
本文介绍了如何使用 Sql Server 2008 从表中删除前 1000 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 中有一个表.我想从中删除前 1000 行.但是,我尝试了此操作,但我并没有删除前 1000 行,而是删除了表中的所有行.

I have a table in SQL Server. I would like to delete the top 1000 rows from it. However, I tried this, but I instead of just deleting the top 1000 rows it deleted all the rows in the table.

代码如下:

delete from [mytab] 
select top 1000 
a1,a2,a3
from [mytab]

推荐答案

您尝试的代码实际上是两个语句.一个 DELETE 后跟一个 SELECT.

The code you tried is in fact two statements. A DELETE followed by a SELECT.

您没有按照什么来定义 TOP.

You don't define TOP as ordered by what.

对于从 CTE 中删除的特定排序标准 或类似的表表达式是最有效的方式.

For a specific ordering criteria deleting from a CTE or similar table expression is the most efficient way.

;WITH CTE AS
(
SELECT TOP 1000 *
FROM [mytab]
ORDER BY a1
)
DELETE FROM CTE

这篇关于如何使用 Sql Server 2008 从表中删除前 1000 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆