0

How can I create a table just like another one on SQL Server 2008 by using a Query, As I tried this query but it doesn't work.

Query:

CREATE TABLE MyTable AS MyOldTable

Error:

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

3 Answers 3

5

CREATE TABLE new_table AS (SELECT * FROM old_table);

Will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
3

Try this

CREATE TABLE MyTable AS (SELECT * FROM myoldtable);

1 Comment

Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('.
0

Using SQL Server Management Studio

From source table contextual menu you select Script Table as > CREATE To > New Query Editor Window: enter image description here

Then you edit the source code changing, for example, the name of table (the new name):

USE [AdventureWorks2012]
GO

/****** Object:  Table [HumanResources].[Employee]    Script Date: 1/4/2014 1:05:14 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [HumanResources].[AuditEmployee]( -- <-- Here you change table name
    [BusinessEntityID] [int] NOT NULL,
    [NationalIDNumber] [nvarchar](15) NOT NULL,
    [LoginID] [nvarchar](256) NOT NULL,
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.