-1

Why do I get an error when I execute the below T-SQL query?

CREATE TABLE TblCustomer 
 AS (SELECT * FROM Customer);

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'

I am trying to create Tblcustomer table same as Customer table

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vimal
  • 20
  • 1
  • 5
  • 1
    https://stackoverflow.com/questions/16683758/how-to-create-a-table-from-select-query-result-in-sql-server-2008 – Mazen Amir Aug 17 '19 at 06:20

2 Answers2

1

If you want to clone your table with data then query in SQL Server is

 select * into table2 from table1

If you want to just copy the table schema not data then query in SQL Server is

 select * into table2 from table1 where 1=0
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DarkRob
  • 3,843
  • 1
  • 10
  • 27
  • It's **important** to note that this is for **SQL Server** - not generally for "sql" ...... SQL is **not** SQL Server - SQL is also used by MySQL, Oracle, Postgres etc. - and some of those systems handle this task differently.... – marc_s Aug 17 '19 at 06:37
0

This is Oracle syntax; in SQL Server try:

select * 
into TblCustomer 
from customer;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Red Devil
  • 2,343
  • 2
  • 21
  • 41