如何在 SQL (MSSQL) 中逐行提交/执行删除

发布于 2024-12-11 11:00:37 字数 730 浏览 4 评论 0原文

我有一个简单的表,但很长(几百万或几行)。 该表包含许多我需要删除的配对行。 行数据不明确! 有单行(没有配对行) 表对由连接到第三列的两列中的交叉信息定义。 我希望每个数据标识符只有一行。 因此,我需要 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 技术交流群。

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

发布评论

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

评论(1

梦冥 2024-12-18 11:00:37

删除column1=column2的情况

DELETE FROM myDB.dbo.myTable
WHERE myIndexColumn in (SELECT myReversedIndex FROM myDB.dbo.myTable)
  AND column1 <> column2

要删除column1=column2

;with cte as
(
    select *,
        row_number() over (
                partition by Column1 + Column2 + Column3
                order by (SELECT 1)
                ) rn
    from yourtable
    where column1 = column2
)
delete cte where rn > 1

CTE也可用于删除所有重复项

;with cte as
(
    select *,
        row_number() over (
                partition by 
                      CASE WHEN Column1 > Column2 THEN Column2 ELSE Column1 END + 
                      CASE WHEN Column1 > Column2 THEN Column1 ELSE Column2 END + 
                      Column3
                order by (SELECT 1)
                ) rn
    from yourtable
)
delete cte where rn > 1

To not delete the cases where column1 = column2

DELETE FROM myDB.dbo.myTable
WHERE myIndexColumn in (SELECT myReversedIndex FROM myDB.dbo.myTable)
  AND column1 <> column2

To remove column1 = column2

;with cte as
(
    select *,
        row_number() over (
                partition by Column1 + Column2 + Column3
                order by (SELECT 1)
                ) rn
    from yourtable
    where column1 = column2
)
delete cte where rn > 1

The CTE can be used to delete all duplicates too

;with cte as
(
    select *,
        row_number() over (
                partition by 
                      CASE WHEN Column1 > Column2 THEN Column2 ELSE Column1 END + 
                      CASE WHEN Column1 > Column2 THEN Column1 ELSE Column2 END + 
                      Column3
                order by (SELECT 1)
                ) rn
    from yourtable
)
delete cte where rn > 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文