I have a need to query a data set that is stored in two different SQL schemas depending on how we receive the data. I have created the two queries to look at dataset 1 and dataset 2 and it formats that data into two separate temp tables (in the same format).
I'm now trying to do the second step which is working out which temp table has been populated putting this data into a new temp table in order to move on to step 3.
To do this, I'm trying to create an IF statement to work out which dataset we receive but can't seem to get the query to work (even though I have seen solutions from other people's similar queries).
This is my code:
IF object_id('tempdb..#Final_Prem') IS NOT NULL 
BEGIN 
    DROP TABLE #Final_Prem 
END
DECLARE @Command varchar(500)
DECLARE @DS1 AS FLOAT
DECLARE @DS2 AS FLOAT
SET @DS1 = (SELECT SUM(PREM) FROM #Dataset1 )
SET @DS2 = (SELECT SUM(PREM) FROM #Dataset2 )
IF (@DS1 IS NULL OR @DS1 = 0)
BEGIN
    SET @Command = 'SELECT * INTO #Final_Prem FROM #Dataset2'
END ELSE BEGIN
    SET @Command = 'SELECT * INTO #Final_Prem FROM #Dataset1'
END
EXECUTE (@Command)
SELECT * FROM #Final_Prem
The error I keep getting is
Msg 208, Level 16, State 0, Line 18
Invalid object name '#Final_Prem'
Any help would be much appreciated, thanks
#Dataset1&#Dataset2isn't a table.SET @DS1 = (SELECT SUM(PREM) FROM #Dataset1 )useSELECT @DS1 = SUM(PREM) FROM #Dataset1