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)行?
基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
