使用SQL Server中的select查询创建表

时间:2014-04-21 12:22:51

标签: sql sql-server

有没有办法在SQL Server中基于SELECT查询创建新表?

我不想手动输入所有字段(表格应根据查询中的字段自动生成)。

我已经尝试过:

CREATE TABLE new_table AS (SELECT * FROM ...);

但它不起作用。

6 个答案:

答案 0 :(得分:3)

您使用的是错误的SQL Server语法。相反,尝试:

SELECT * INTO new_table FROM old_table 

答案 1 :(得分:3)

CREATE TABLE NEW_TABLE
AS
SELECT employee_id, last_name,
FROM
employees

答案 2 :(得分:1)

你可以这样继续

select * into NewTable from  oldtable 

答案 3 :(得分:0)

Maybe you could try like this in your query:

create table #temp(@field1 int,@field2 varchar(10))

insert into #temp

       select a,b from tableFrom



select * from #a

然后,当你不再使用该表时,

drop table #a

答案 4 :(得分:0)

我必须为select中的列添加别名,但如果没有名称,我必须认为当前查询对字段别名。

select
  *
into #tempabc
from (
  Select 'a' [test]
  union
  Select 'b' [test]
) a

select * from #tempabc

答案 5 :(得分:-1)

SELECT * INTO new_table FROM old_table where 1=2

其中condition应该像你的查询不应该返回数据