Greetings fellow SQL Server Experts...
I have a challenge where I have a need to dynamically create a Temp Table from a set of (2) views; so I can create an Index on it. I tried to use Schema Binding with the views; but seemed to have issues since the views are complex with many aggregate/custom functions.
Anyway...
I have (2) different Views that I am attempting to set into a Temp table so I can index a field for performance in a Join.
How can I get the below to work? I've tried many approaches without success! Can it be done?
Error is ...
Cannot find the object "#Rollup_Primary_AAAA" because it does not exist or you do not have permissions.
Ugh!!!!
Thanks in Advance (As Always!)
Steve Medvid
IT Consultant & Web Master
Chester County, PA Residents
Please Show Your Support...
I have a challenge where I have a need to dynamically create a Temp Table from a set of (2) views; so I can create an Index on it. I tried to use Schema Binding with the views; but seemed to have issues since the views are complex with many aggregate/custom functions.
Anyway...
I have (2) different Views that I am attempting to set into a Temp table so I can index a field for performance in a Join.
How can I get the below to work? I've tried many approaches without success! Can it be done?
Code:
DECLARE @SQL NVARCHAR(4000)
DECLARE @CAT NVARCHAR(4)
DECLARE @Rollup_Flag VARCHAR(1)
SET @CAT = 'AAAA'
SET @Rollup_Flag = 'Y'
--Basically (2 Different Views Used based on Flag!)
IF @CAT = 'AAAA' AND @Rollup_Flag = 'Y'
-- Use VIEW 1
BEGIN
SET @SQL = 'SELECT * INTO #Rollup_Primary_AAAA FROM vw_tblCalc_AAAA_Primary'
EXEC sp_executesql @SQL
-- EXEC (@SQL)
CREATE CLUSTERED INDEX idx_Primary_AAAA ON #Rollup_Primary_AAAA ([S_Id])
END
ELSE IF @CAT = 'AAAA' AND @Rollup_Flag = 'N'
-- Use VIEW 2
BEGIN
SET @SQL = 'SELECT * INTO #Rollup_Primary_AAAA FROM vw_tblCalc_AAAA_Primary_S'
EXEC sp_executesql @SQL
-- EXEC (@SQL)
CREATE CLUSTERED INDEX idx_Primary_AAAA ON #Rollup_Primary_RHEQ ([STraunche_Id])
END
SELECT COUNT(*) FROM #Rollup_Primary_AAAA
Error is ...
Cannot find the object "#Rollup_Primary_AAAA" because it does not exist or you do not have permissions.
Ugh!!!!
Thanks in Advance (As Always!)
Steve Medvid
IT Consultant & Web Master
Chester County, PA Residents
Please Show Your Support...