T-SQL删除插入的记录

发布于 2024-12-28 13:12:02 字数 358 浏览 3 评论 0原文

我知道标题可能看起来很奇怪,但这就是我想做的:

  1. 我有包含许多记录的表。
  2. 我想获取其中一些记录并将它们插入到其他表中。像这样的事情:

    INSERT INTO TableNew SELECT * FROM TableOld WHERE ...

  3. 棘手的部分是我希望我插入的这些行也从原始表中删除。 p>

有没有一种简单的方法可以做到这一点,因为我唯一想到的就是使用临时表来保存所选记录,然后将它们放入第二个表中并从第一个表中删除与它们匹配的行。这是一个解决方案,但是有这么多记录(超过 300 万条半),我正在寻找其他想法......

I know the title may seem strange but this is what I want to do:

  1. I have table with many records.
  2. I want to get some of this records and insert them in other table. Something like this:

    INSERT INTO TableNew SELECT * FROM TableOld WHERE ...

  3. The tricky part is that I want this rows that I have inserted to be deleted form the origin table as well.

Is there a easy way to do this, because the only think that I have managed to do is to use a temporary table for saving the selected records and then to put them in the second table and delete rows that match with them from the first table. It is a solution, but with so many records (over 3 millions and half) I am looking for some other idea...

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

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

发布评论

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

评论(3

何处潇湘 2025-01-04 13:12:02

在 2005 年以上,使用 OUTPUT 子句如下:

DELETE FROM TableOld 
OUTPUT DELETED.* INTO TableNew
WHERE YourCondition

它将在单个事务中执行,并且同时完成或回滚

In 2005+ use OUTPUT clause like this:

DELETE FROM TableOld 
OUTPUT DELETED.* INTO TableNew
WHERE YourCondition

It will be performed in single transaction and either completed or roll back simultaneously

慵挽 2025-01-04 13:12:02

您可以使用 insert ...output 子句来存储 ID临时表中复制的行的数量。然后您可以根据临时表从原始表中删除行。

declare @Table1 table (id int, name varchar(50))
declare @Table2 table (id int, name varchar(50))

insert @Table1 (id,name)
          select 1, 'Mitt'
union all select 2, 'Newt'
union all select 3, 'Rick'
union all select 4, 'Ron'


declare @copied table (id int)

insert  @Table2
        (id, name)
output  inserted.id 
into    @copied
select  id
,       name
from    @Table1
where   name <> 'Mitt'

delete  @Table1
where   id in 
        (
        select  id 
        from    @copied
        )
        
select  *
from    @Table1

数据资源管理器中的工作示例。

You can use the insert ... output clause to store the ID's of the copied rows in a temporary table. Then you can delete the rows from the original table based on the temporary table.

declare @Table1 table (id int, name varchar(50))
declare @Table2 table (id int, name varchar(50))

insert @Table1 (id,name)
          select 1, 'Mitt'
union all select 2, 'Newt'
union all select 3, 'Rick'
union all select 4, 'Ron'


declare @copied table (id int)

insert  @Table2
        (id, name)
output  inserted.id 
into    @copied
select  id
,       name
from    @Table1
where   name <> 'Mitt'

delete  @Table1
where   id in 
        (
        select  id 
        from    @copied
        )
        
select  *
from    @Table1

Working example at Data Explorer.

╰沐子 2025-01-04 13:12:02

你应该做这样的事情:

INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2" 
WHERE ...

DELETE FROM "table1"
WHERE ...

You should do some thing like this:

INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2" 
WHERE ...

DELETE FROM "table1"
WHERE ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文