将 SQL 数据从一个表移动到另一个表 [英] Move SQL data from one table to another

查看:45
本文介绍了将 SQL 数据从一个表移动到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将所有行数据从一个表移动到另一个表,匹配某个查询?

I was wondering if it is possible to move all rows of data from one table to another, that match a certain query?

例如,我需要将所有表行从 Table1 移动到 Table2,其中它们的用户名 = 'X' 和密码 = 'X',以便它们不再出现在 Table1 中.

For example, I need to move all table rows from Table1 to Table2 where their username = 'X' and password = 'X', so that they will no longer appear in Table1.

我使用的是 SQL Server 2008 Management Studio.

I'm using SQL Server 2008 Management Studio.

推荐答案

应该可以在一个事务中使用两个语句,一个插入和一个删除:

Should be possible using two statements within one transaction, an insert and a delete:

BEGIN TRANSACTION;
INSERT INTO Table2 (<columns>)
SELECT <columns>
FROM Table1
WHERE <condition>;

DELETE FROM Table1
WHERE <condition>;

COMMIT;

这是最简单的形式.如果您必须担心在两个语句之间将新的匹配记录插入到 table1 中,您可以添加一个 并存在 .

This is the simplest form. If you have to worry about new matching records being inserted into table1 between the two statements, you can add an and exists <in table2>.

这篇关于将 SQL 数据从一个表移动到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆