SQL Server - How to create a new table using a select from another table (both in the same database)

0

I made a sql script by joining some tables in SQL Server and I will have the result in some fields, but I would like to create a new table and copy everything I have filtered with select into it ... Something like: create table Enderecos_new as my sql script ....).

Would anyone know how to help me?

Thank you:)

Select 
loc.loc_nu_sequencial, 

CASE
	WHEN
		loc.cep IS NULL
	THEN logr.cep 
	WHEN
		logr.cep IS NULL
	THEN loc.cep 
	ELSE loc.cep
END AS cep,

 logr.log_nome,
 logr.log_complemento,
 log_bairro.bai_no,
 loc.loc_no,
 loc.ufe_sg 
  
  from log_localidade as loc

  left join log_logradouro as logr on loc.loc_nu_sequencial = logr.loc_nu_sequencial
  left join log_bairro on logr.bai_nu_sequencial_ini = log_bairro.bai_nu_sequencial

  order by loc_nu_sequencial
    
asked by anonymous 16.10.2017 / 22:29

1 answer

2

Use the INTO clause in your code. The name entered in it will be used when creating the new table.

SELECT 
    loc.loc_nu_sequencial, 
    CASE
        WHEN loc.cep IS NULL THEN logr.cep 
        WHEN logr.cep IS NULL THEN loc.cep 
        ELSE loc.cep
    END AS cep,
    logr.log_nome,
    logr.log_complemento,
    log_bairro.bai_no,
    loc.loc_no,
    loc.ufe_sg 
INTO nova_tabela
FROM log_localidade AS loc
    LEFT JOIN log_logradouro as logr 
        ON loc.loc_nu_sequencial = logr.loc_nu_sequencial
    LEFT JOIN log_bairro 
        ON logr.bai_nu_sequencial_ini = log_bairro.bai_nu_sequencial
ORDER BY loc_nu_sequencial

link

    
16.10.2017 / 22:47