简体   繁体   English

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

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

I have something like this 我有这样的东西

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

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

I get an error like this: 我收到这样的错误:

Incorrect syntax near ')'. ')'附近的语法不正确。

My first attempt with temporary tables. 我第一次尝试使用临时表。 So what is the mistake here? 那么这是什么错误呢?

EDIT: 编辑:
Drop and recreate if temporary table exists. 如果存在临时表,则删除并重新创建。 Getting error 遇到错误

Msg 2714, Level 16, State 6, Line 55 消息2714,第16级,州立6,第55行
There is already an object named '#TempHourlyTable' in the database. 数据库中已经有一个名为“ #TempHourlyTable”的对象。

You need to alias your derived table (subquery), like: 您需要为派生表(子查询)添加别名,例如:

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

You can also: 你也可以:

select top 10 * 
into #temp
from customers

try this 尝试这个

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

what are you looking to do with the temp. 您打算如何处理这个温度。 table? 表?

i find using CTEs much more convenient 我发现使用CTE更方便

CTEs 热膨胀系数

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

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