繁体   English   中英

从SQL Server中的子查询创建临时表导致错误

[英]Create a temporary table from a subquery in sql server causing error

我有这样的东西

 if object_id('tempdb.#TempHourlyTable') is not null
drop table #TempHourlyTable

 select * into #TempHourlyTable
 from (
         select top 10 * from customers
      )

我收到这样的错误:

')'附近的语法不正确。

我第一次尝试使用临时表。 那么这是什么错误呢?

编辑:
如果存在临时表,则删除并重新创建。 遇到错误

消息2714,第16级,州立6,第55行
数据库中已经有一个名为“ #TempHourlyTable”的对象。

您需要为派生表(子查询)添加别名,例如:

select * into #TEmpTable
 from (
         select top 10 * from customers
      ) as [SomeAlias]

你也可以:

select top 10 * 
into #temp
from customers

尝试这个

insert into #TEmpTable 
select top (10) * from customers

您打算如何处理这个温度。 表?

我发现使用CTE更方便

热膨胀系数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM