Servidor SQL copiar todas as linhas de uma tabela para outra tabela I. E. duplicada

Quero manter uma mesa como história e substituí-la por uma vazia. Como posso fazer isto através do Estúdio de gestão?

Author: Sandor Drieënhuizen, 2010-04-22

6 answers

Duplique a sua mesa numa tabela para ser arquivada:

SELECT * INTO ArchiveTable FROM MyTable

Apague todos os itens na sua tabela:

DELETE * FROM MyTable
 24
Author: froadie, 2015-09-29 08:41:43
select * into x_history from your_table_here;
truncate table your_table_here;
 20
Author: Michael Buen, 2010-04-22 14:04:48
Não tenho servidor sql por perto para testar, mas acho que é apenas ...
insert into newtable select * from oldtable;
 13
Author: Hans Olsson, 2010-04-22 14:02:17

Pode usar SQL RAW:

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

Ou usar o assistente:

  1. carregue com o botão direito na Base de dados - > Tarefas - >Exportar Dados
  2. seleccione a base de dados de origem/destino
  3. seleccione a tabela de origem/destino e campos
  4. copiar os dados

Depois executar:

TRUNCATE TABLE SOURCE_TABLE
 8
Author: ntziolis, 2010-04-22 14:08:28

Tente este comando único para apagar e inserir os dados:

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

Amostra de trabalho:

--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

Resultado:

(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)
 2
Author: KM., 2010-04-22 14:09:29
Isto vai funcionar.
select * into DestinationDatabase.dbo.[TableName1] from (
Select * from sourceDatabase.dbo.[TableName1])Temp
 -2
Author: Anup Ghosh, 2013-10-02 08:26:17