T-SQL删除插入的记录
我知道标题可能看起来很奇怪,但这就是我想做的:
- 我有包含许多记录的表。
我想获取其中一些记录并将它们插入到其他表中。像这样的事情:
INSERT INTO TableNew SELECT * FROM TableOld WHERE ...
棘手的部分是我希望我插入的这些行也从原始表中删除。 p>
有没有一种简单的方法可以做到这一点,因为我唯一想到的就是使用临时表来保存所选记录,然后将它们放入第二个表中并从第一个表中删除与它们匹配的行。这是一个解决方案,但是有这么多记录(超过 300 万条半),我正在寻找其他想法......
I know the title may seem strange but this is what I want to do:
- I have table with many records.
I want to get some of this records and insert them in other table. Something like this:
INSERT INTO TableNew SELECT * FROM TableOld WHERE ...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 2005 年以上,使用
OUTPUT
子句如下:它将在单个事务中执行,并且同时完成或回滚
In 2005+ use
OUTPUT
clause like this:It will be performed in single transaction and either completed or roll back simultaneously
您可以使用 insert ...output 子句来存储 ID临时表中复制的行的数量。然后您可以根据临时表从原始表中删除行。
数据资源管理器中的工作示例。
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.
Working example at Data Explorer.
你应该做这样的事情:
You should do some thing like this: