Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error with my SQL Create Table As [duplicate]

I am trying to create a new table in Microsoft SQL Server Management Studio based on two existing tables.

When I execute the query below, I get an error saying that there is an:

Incorrect syntax near the keyword 'SELECT'.

SQL code:

CREATE TABLE NEW_TABLE AS
 SELECT OLD_TABLE.A
    , OLD_TABLE.B
    , OTHER_OLD_TABLE.C
 FROM OLD_TABLE
 INNER JOIN OTHER_OLD_TABLE
 ON OLD_TABLE.A = OTHER_OLD_TABLE.D;

I looked at various other problems, but could not find a solution to mine. Do you have any idea what could be wrong with the syntax?

like image 338
Smile5.09 Avatar asked Sep 03 '25 09:09

Smile5.09


1 Answers

Alternatively, you can use SELECT * INTO new_table statement just like this.

SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C INTO NEW_TABLE
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;

this statement will also create a new table as you required.

like image 108
Thiha Zaw Avatar answered Sep 05 '25 00:09

Thiha Zaw