首页 文章

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

提问于
浏览
147

这个问题在这里已有答案:

我想在SQL Server中从select查询结果创建一个表,我试过了

create table temp AS select.....

但是我收到了一个错误

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

6 回答

  • 25

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

    Select * into new_table  from  old_table
    
  • 8

    尝试使用SELECT INTO ....

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

    从[源表]中选择[新表]中的[字段名称]

  • 2

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

    并不总是和MYSQL相同: "create table temp AS select.."

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

    例如 :

    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
    

    并不总是屈服:

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

    但可能是:

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

    请试试:

    SELECT * INTO NewTable FROM OldTable
    
  • 50

    使用 SELECT...INTO

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

    例,

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

    标准语法,

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

相关问题