Create new table from another in sql server

Asked

Viewed 6,140 times

0

I’m doing this query:

CREATE TABLE nova_tabela AS
SELECT * FROM tabela_copiada;

I’m getting the following error:

Incorrect syntax near the keyword 'SELECT'

Already researched the syntax seems to me correct. Someone can help me?

  • temporary or physical table ?

  • 1

    @Leonardosilva The construction CREATE TABLE ... AS (SELECT ... FROM ...) works in some database managers but not in SQL Server.

1 answer

2

The correct syntax is

SELECT colunas 
INTO tabelaNova 
FROM TabelaQueDesejaCopiar 
WHERE Condicao

SELECT... INTO creates a new table in the default file group and inserts on the lines resulting from the consultation

Microsoft Docs

Your select would look like this:

SELECT * INTO nova_tabela FROM tabela_copiada
  • Complementing, when you want to create the new table with the same structure but without data, just use restriction that returns false; for example: WHERE 1 = 0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.