Copy data from one table to another without typing field by field

3

Does anyone know the SQL command that I copy data from one table to another table, without explicitly specifying all fields in the original table? I know there is a way but I forgot how it does and I could not find it on the net, if anyone knows, I would be very grateful if you could share it here with me, thank you for your attention. #SQL

    
asked by anonymous 08.03.2016 / 20:40

3 answers

2

I was able to resolve using SELECT * INTO , thank you all for your attention.

    
10.03.2016 / 20:48
2

You can do it in 3 ways.

-- sql tabela volátil só existira em tempo de execução
declare @HIERARQUIA table
(
    [ID_HIERARQUIA] [int]  NOT NULL,
    [MICRO] [int] NOT NULL,
    [DESCR] [varchar](250) NOT NULL,
    [MACRO] [int] NULL,
    [POSICAO] [varchar](250) NOT NULL
)

-- 1ª 
-- copia para tabela temporaria ...  ficar no banco tempdb até que finalize o execução.
select * into #HIERARQUIA from HIERARQUIA ;

-- 2ª 
-- copia para tabela volátil só existira em tempo de execução
insert into @HIERARQUIA
select * from #HIERARQUIA;

-- 3ª 
-- copia para uma tabela da base de dados
insert into HIERARQUIA
select * from HIERARQUIA ;

In short. You will need to create a table with the same fields or use a temporary one and use * in Select , this will map your fields to another table automatically.

    
10.03.2016 / 20:34
1

In SQL Server and other databases you can use select into (with some syntax differences)

SELECT * INTO nova_tabela FROM outra_tabela

Inserting Rows by Using SELECT INTO

    
08.03.2016 / 21:04