如何删除SQL 2000中的TOP n结果?

发布于 2024-07-26 06:51:28 字数 168 浏览 4 评论 0原文

我知道在 SQL Server 2005 中我们可以这样做:

从 tbX 中删除前 10 个,其中 X = 1

考虑主键由两个 FK 列组成,在 SQL2K 中执行相同操作的查询可能是什么?

I know in SQL Server 2005 we could do something like:

DELETE TOP 10 FROM tbX where X = 1

What could be the query to do the same in SQL2K, considering that the primary key is composed by two FK columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人骨 2024-08-02 06:51:28

哈克,但是:

SET ROWCOUNT 10
DELETE FROM tbX where X = 1
SET ROWCOUNT 0

不过,我不会自己写这个;-p

(我要去洗手......)

hacky, but:

SET ROWCOUNT 10
DELETE FROM tbX where X = 1
SET ROWCOUNT 0

I wouldn't write this myself, though ;-p

(I'm off to wash my hands...)

我早已燃尽 2024-08-02 06:51:28

这在 SQL2012 中对我有用:

;with window as (select top (12) * from PromoterQueue order by LogId)
delete from window 

This works for me in SQL2012:

;with window as (select top (12) * from PromoterQueue order by LogId)
delete from window 
只为守护你 2024-08-02 06:51:28
DELETE
FROM tbX
WHERE id IN (SELECT TOP 10 id FROM tbX WHERE X = 1)

您必须先删除外键记录。

DELETE
FROM tbX
WHERE id IN (SELECT TOP 10 id FROM tbX WHERE X = 1)

You would have to delete the foreign key records first.

烛影斜 2024-08-02 06:51:28

这不是有效的 SQL,但类似的东西可能有效:

DELETE t
from tbx t 
join (
  select top 10 k1,k2 from tbx
) as t2 on t.k1 = t2.k1 and t.k2 = t2.k2

注意:如果 TOP 在子查询中不起作用,只需将内容插入到临时表中并连接。

This is not working SQL but something along the lines may work:

DELETE t
from tbx t 
join (
  select top 10 k1,k2 from tbx
) as t2 on t.k1 = t2.k1 and t.k2 = t2.k2

Note: If TOP does not work in the subquery just insert the stuff into a temp table and join.

抱着落日 2024-08-02 06:51:28

当处理复合键时,我会像这样删除。

delete from tblX
from tblx as aa
join
(
  select top 10 * from tblX
) as bb
on
  aa.key1 = bb.key1
  and
  aa.key2 = bb.key2

when dealing with a composite key I would delete like this.

delete from tblX
from tblx as aa
join
(
  select top 10 * from tblX
) as bb
on
  aa.key1 = bb.key1
  and
  aa.key2 = bb.key2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文