选择复杂select语句的结果s到新表中

时间:2015-04-30 12:11:45

标签: sql-server

我有一个选择陈述

(select * from xxxx where yyy = '2011') AS a
INNER JOIN
    (select * from xxx where yyy= '2010') AS b
ON a.yyy = b.yyy AND a.xxx = b.xxx)

有效。

如何将只有一列的结果插入另一个表?

我尝试用select.... INTO xxx包含此内容,但它抱怨语法

2 个答案:

答案 0 :(得分:0)

虽然您的特定查询不会返回任何结果,并且语法也是错误的,但如果您正在寻找一种方法将数据从SELECT语句插入(临时)表中,您可以执行以下操作:

SELECT your_columns
INTO new_table
--or you can use a temp table
--INTO #table
FROM another_table

所以在你的情况下,它会是这样的:

SELECT *
INTO new_table
FROM
 (select * from xxxx where yyy = '2011') AS a
INNER JOIN
    (select * from xxx where yyy= '2010') AS b
ON a.yyy = b.yyy AND a.xxx = b.xxx

答案 1 :(得分:0)

SELECT * INTO new_table_name FROM table_name应该可以正常使用。

如果你尝试输出是什么:

SELECT tmp.* INTO new_table_name FROM
(
    (SELECT * FROM xxxx WHERE yyy = '2011') AS a
    INNER JOIN
    (SELECT * FROM xxx WHERE yyy= '2010') AS b
    ON a.yyy = b.yyy AND a.xxx = b.xxx
)   tmp