I need to alter one view and I want to introduce 2 temporary table before the SELECT.
Is this possible? And how can I do it?
ALTER VIEW myView
AS
SELECT *
INTO #temporary1
SELECT *
INTO #temporary2
SELECT * FROM #temporary1
UNION ALL
SELECT * FROM #temporary1
DROP TABLE #temporary1
DROP TABLE #temporary2
When I attempt this it complains that ALTER VIEW must be the only statement in the batch.
How can I achieve this?
No, a view consists of a single
SELECTstatement. You cannot create or drop tables in a view.Maybe a common table expression (CTE) can solve your problem. CTEs are temporary result sets that are defined within the execution scope of a single statement and they can be used in views.
Example (taken from here) – you can think of the
SalesBySalesPersonCTE as a temporary table:Performance considerations
Which are more performant, CTE or temporary tables?