如何从 SQL Server 2008 中的选择查询结果创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16683758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 15:21:30  来源:igfitidea点击:

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

sqlsql-serversql-server-2008

提问by yogesh9239

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

我想从 SQL Server 中的选择查询结果创建一个表,我试过了

create table temp AS select.....

but I got an error

但我有一个错误

Incorrect syntax near the keyword 'AS'

关键字“AS”附近的语法不正确

回答by Sanjeev Rai

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

使用以下语法从 SQL Server 2008 中的旧表创建新表

Select * into new_table  from  old_table 

回答by John Woo

use SELECT...INTO

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.

SELECT INTO 语句创建一个新表并用 SELECT 语句的结果集填充它。SELECT INTO 可用于将来自多个表或视图的数据合并到一个表中。它还可用于创建包含从链接服务器中选择的数据的新表。

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]

回答by mssql-mysql

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

请小心,MSSQL: "SELECT * INTO NewTable FROM OldTable"

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

并不总是与 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.

我认为有时这(在 MSSQL 中)并不能保证新表中的所有字段都与旧表的类型相同。

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)

回答by Rebika

Try using SELECT INTO....

尝试使用 SELECT INTO ....

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

回答by TechDo

Please try:

请尝试:

SELECT * INTO NewTable FROM OldTable

回答by Prabhash Jha

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