如何在 SQL (MSSQL) 中逐行提交/执行删除
我有一个简单的表,但很长(几百万或几行)。 该表包含许多我需要删除的配对行。 行数据不明确! 有单行(没有配对行) 表对由连接到第三列的两列中的交叉信息定义。 我希望每个数据标识符只有一行。 因此,我需要 myTable 在满足条件时立即收缩。 我尝试过:
myIndexColumn = Column1 + Column2 + Column3
myReversedIndexColumn = Column2 + Column1 + Column3
CREATE NONCLUSTERED INDEX myIndex1 ON myDB.dbo.myTable (
myIndexColumn ASC
)
CREATE NONCLUSTERED INDEX myIndex2 ON myDB.dbo.myTable (
myReversedIndexColumn ASC
)
DELETE FROM myDB.dbo.myTable
WHERE myIndexColumn in (SELECT myReversedIndex FROM myDB.dbo.myTable)
问题是两个配对数据都被删除,而不是留下一行数据。
显然,这是因为 DELETE 仅在运行整个事务后才会提交更改。
如果我可以说服 MS SQL 2008 R2 Express 版本在满足条件时提交 DELETE
,则 SELECT
子句将在要删除的每个行测试上输出更短的列表。
我该怎么做?
I have a simple table but a long one (few millions or rows).
The table contains many paired rows which I need to delete.
The row data is not distinct!
There are single rows (which has no a paired row)
The table pairs are defined by cross info in two columns concatenated to a 3rd column.
I would like to have only one row of each data identifier.
Therefore, I need the myTable to shrink immediately whereis a condition is met.
I tried:
myIndexColumn = Column1 + Column2 + Column3
myReversedIndexColumn = Column2 + Column1 + Column3
CREATE NONCLUSTERED INDEX myIndex1 ON myDB.dbo.myTable (
myIndexColumn ASC
)
CREATE NONCLUSTERED INDEX myIndex2 ON myDB.dbo.myTable (
myReversedIndexColumn ASC
)
DELETE FROM myDB.dbo.myTable
WHERE myIndexColumn in (SELECT myReversedIndex FROM myDB.dbo.myTable)
The problem is that both paired data is deleted instead of leaving one row of the data.
Obviously, that is because the DELETE
commits changes only after running the entire transaction.
If I could persuid the MS SQL 2008 R2 Express edition to commit the DELETE
upon condition is met, the SELECT
clause would have output a shorter list on each row test to delete.
How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不删除column1=column2的情况
要删除column1=column2
CTE也可用于删除所有重复项
To not delete the cases where column1 = column2
To remove column1 = column2
The CTE can be used to delete all duplicates too