문제

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
There is already an object named '#TempHourlyTable' in the database.

도움이 되었습니까?

해결책

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

CTEs

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top