Creating temporary view from a temporary table in SQL Server

Thunder picture Thunder · Aug 25, 2011 · Viewed 46.1k times · Source

I have a temporary table and I would like to create a temporary view over this temporary table.

Is it possible?

In following example I would like #Top10Records to be a view instead of a table so that I get

select * into #Top10Records from (select top 10 * from #MytempTable)

Answer

DarylChymko picture DarylChymko · Aug 25, 2011

You can use a Common Table expression to do that:

WITH Top10Records  AS 
( 
 select top 10 * from #MytempTable
) 
SELECT * FROM Top10Records 
GO