SQL Server 将一个表中的所有行复制到另一个表中,即重复表

发布于 2024-08-30 01:32:20 字数 58 浏览 4 评论 0原文

我想保留一张桌子作为历史并用空桌子替换它。我如何通过 Management Studio 执行此操作?

I want to keep a table as history and replace it with an empty one. How can I do this through Management Studio?

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

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

发布评论

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

评论(6

并安 2024-09-06 01:32:20

将表复制到要存档的表中:

SELECT * INTO ArchiveTable FROM MyTable

删除表中的所有条目:

DELETE * FROM MyTable

Duplicate your table into a table to be archived:

SELECT * INTO ArchiveTable FROM MyTable

Delete all entries in your table:

DELETE * FROM MyTable
无力看清 2024-09-06 01:32:20

没有 sql server 可供测试,但我认为这只是:

insert into newtable select * from oldtable;

Don't have sql server around to test but I think it's just:

insert into newtable select * from oldtable;
羅雙樹 2024-09-06 01:32:20
select * into x_history from your_table_here;
truncate table your_table_here;
select * into x_history from your_table_here;
truncate table your_table_here;
远山浅 2024-09-06 01:32:20

您可以使用 RAW SQL:

INSERT INTO DEST_TABLE (Field1, Field2) 
SELECT Source_Field1, Source_Field2 
FROM SOURCE_TABLE

或者使用向导:

  1. 右键单击数据库 ->任务
    ->导出数据
  2. 选择源/目标数据库
  3. 选择源/目标表并
    fields
  4. 复制数据

然后执行:

TRUNCATE TABLE SOURCE_TABLE

Either you can use RAW SQL:

INSERT INTO DEST_TABLE (Field1, Field2) 
SELECT Source_Field1, Source_Field2 
FROM SOURCE_TABLE

Or use the wizard:

  1. Right Click on the Database -> Tasks
    -> Export Data
  2. Select the source/target Database
  3. Select source/target table and
    fields
  4. Copy the data

Then execute:

TRUNCATE TABLE SOURCE_TABLE
_失温 2024-09-06 01:32:20

尝试这个单个命令来删除和插入数据

DELETE MyTable
    OUTPUT DELETED.Col1, DELETED.COl2,...
        INTO MyBackupTable

工作示例:

--set up the tables
DECLARE @MyTable table (col1 int, col2 varchar(5))
DECLARE @MyBackupTable table (col1 int, col2 varchar(5))
INSERT INTO @MyTable VALUES (1,'A')
INSERT INTO @MyTable VALUES (2,'B')
INSERT INTO @MyTable VALUES (3,'C')
INSERT INTO @MyTable VALUES (4,'D')

--single command that does the delete and inserts
DELETE @MyTable
    OUTPUT DELETED.Col1, DELETED.COl2
        INTO @MyBackupTable

--show both tables final values
select * from @MyTable
select * from @MyBackupTable

输出:

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(4 row(s) affected)
col1        col2
----------- -----

(0 row(s) affected)

col1        col2
----------- -----
1           A
2           B
3           C
4           D

(4 row(s) affected)

try this single command to both delete and insert the data:

DELETE MyTable
    OUTPUT DELETED.Col1, DELETED.COl2,...
        INTO MyBackupTable

working sample:

--set up the tables
DECLARE @MyTable table (col1 int, col2 varchar(5))
DECLARE @MyBackupTable table (col1 int, col2 varchar(5))
INSERT INTO @MyTable VALUES (1,'A')
INSERT INTO @MyTable VALUES (2,'B')
INSERT INTO @MyTable VALUES (3,'C')
INSERT INTO @MyTable VALUES (4,'D')

--single command that does the delete and inserts
DELETE @MyTable
    OUTPUT DELETED.Col1, DELETED.COl2
        INTO @MyBackupTable

--show both tables final values
select * from @MyTable
select * from @MyBackupTable

OUTPUT:

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(4 row(s) affected)
col1        col2
----------- -----

(0 row(s) affected)

col1        col2
----------- -----
1           A
2           B
3           C
4           D

(4 row(s) affected)
扭转时空 2024-09-06 01:32:20

这将起作用:

select * into DestinationDatabase.dbo.[TableName1] from (
Select * from sourceDatabase.dbo.[TableName1])Temp

This will work:

select * into DestinationDatabase.dbo.[TableName1] from (
Select * from sourceDatabase.dbo.[TableName1])Temp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文