This question already has answers here:
How to create table using select query in SQL Server? (5 answers)
Closed 2 days ago.
I wanted to create a new table from an existing table however, at present, I get an error on the SELECT
keyword, I've tried aliasing it but that didn't work.
I've tried aliasing it and checking the columns to see if I had misspelt anything or if I was calling the correct database or not and everything seems to be in order.
SQL:
create table testTable as
select CompanyName as customer_companyname
, contactname
from Customers;
Error:
Incorrect syntax near the keyword 'select'.
1条答案
按热度按时间yyyllmsg1#
You can't create a table via
create table ... select ...
syntax.You can use
select ... into ...
to create a table with a similar schema to the source table, containing the selected data; but it won't be identical to the original table (e.g. things like keys will be missing). Select Into DocsOr you can use a Create Table ... statement with a suitable definition to create the table, followed by Insert Into ... to populate the data in that table.