How to create a table from select query result in SQL Server 2008

SqlSql ServerSql Server-2008

Sql Problem Overview


I want to create a table from select query result in SQL Server, I tried

create table temp AS select.....

but I got an error

> Incorrect syntax near the keyword 'AS'

Sql Solutions


Solution 1 - Sql

Use following syntax to create new table from old table in SQL server 2008

Select * into new_table  from  old_table 

Solution 2 - Sql

use SELECT...INTO

> The SELECT INTO statement creates a new table and populates it with > the result set of the SELECT statement. SELECT INTO can be used to > combine data from several tables or views into one table. It can also > be used to create a new table that contains data selected from a > linked server.

Example,

SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM   tablename

Standard Syntax,

SELECT  col1, ....., col@      -- <<== select as many columns as you want
        INTO [New tableName]
FROM    [Source Table Name]

Solution 3 - Sql

Please be careful, MSSQL: "SELECT * INTO NewTable FROM OldTable"

is not always the same as MYSQL: "create table temp AS select.."

I think that there are occasions when this (in MSSQL) does not guarantee that all the fields in the new table are of the same type as the old.

For example :

create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable

does not always yield:

create table newTable (field1 varchar(10), field2 integer, field3 float)

but may be:

create table newTable (field1 varchar(10), field2 integer, field3 integer)

Solution 4 - Sql

Please try:

SELECT * INTO NewTable FROM OldTable

Solution 5 - Sql

Try using SELECT INTO....

SELECT ....
INTO     TABLE_NAME(table you want to create)
FROM source_table

Solution 6 - Sql

Select [Column Name] into [New Table] from [Source Table]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionyogesh9239View Question on Stackoverflow
Solution 1 - SqlSanjeev RaiView Answer on Stackoverflow
Solution 2 - SqlJohn WooView Answer on Stackoverflow
Solution 3 - Sqlmssql-mysqlView Answer on Stackoverflow
Solution 4 - SqlTechDoView Answer on Stackoverflow
Solution 5 - SqlRebikaView Answer on Stackoverflow
Solution 6 - SqlPrabhash JhaView Answer on Stackoverflow