How to Delete Top(N) rows with an inner join?(如何使用内部联接删除前(N)行?)
问题描述
我正在尝试使用以下查询从两个表中删除几行
I am trying to delete a few rows from two tables using the following query
Delete top(3) ss
from stage.SubmitItemData ss
INNER JOIN stage.SubmitItems s (NOLOCK) on ss.SubmitItemId = s.SubmitItemId
where s.AgencyCode = 'NC0860000' and s.StatusId = 8
如果我删除参数 s.AgencyCode 和 s.StatusId ,我感到困惑的地方是查询执行没有问题.但是,如果我添加这些参数,我会影响 (0) 行.
Where I am stumped is if I remove the parameters s.AgencyCode and s.StatusId the query executes with no issue. However if I add these parameters I get the (0) rows affected.
我要做的就是控制在任何给定时间删除的记录数.top(n) 不是最好的方法,因为它看起来好像需要订购才能工作?为这种类型的删除创建一个循环会更好吗?
All I am trying to do is to control the number of records deleted at any given time. Is top(n) not the best approach as it looks as if it requires ordering to work? Would it be better to create a loop for this type of delete?
感谢您的任何建议.
推荐答案
DELETE TOP (3)
FROM stage.SubmitItemData
WHERE
EXISTS (SELECT 1
FROM stage.SubmitItems
WHERE SubmitItemId = SubmitItemData.SubmitItemId
AND AgencyCode = 'NC0860000'
AND StatusId = 8)
或者你可以这样做......
Or you could do something like this......
DELETE TOP(3) FROM ss
FROM stage.SubmitItemData ss
INNER JOIN stage.SubmitItems s WITH (NOLOCK)
ON ss.SubmitItemId = s.SubmitItemId
where s.AgencyCode = 'NC0860000' and s.StatusId = 8
这篇关于如何使用内部联接删除前(N)行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用内部联接删除前(N)行?
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
