简体   繁体   中英

Can not drop a temp table Invalid object name '#Temp1' error - in sql server

I have the following code which throws an Invalid object name '#Temp1' error....

Each iteration, i truncate the table (DROP table also did not work)....nothing obvious jumps out as a problem.....thanks for any ideas...

While @@Fetch_Status = 0 -- For each ObsSetCode - 
            BEGIN           

                PRINT 'I am processing the SECTION ------ ' + @dl_EventSetName
                PRINT 'I am processing the ObsSetCode ------ ' + @ObsSetList

                IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL
                BEGIN
                    TRUNCATE TABLE #Temp1
                END

                    Insert Into #Temp1 
                    Select o.EventSetName,
                           o.EventSetDisplay,
                           o.EventSetDescription,
                           o.ChildSetName,
                           ROW_NUMBER() Over (Order By o.ChildSetName) RN                  
                    From   ##ObsSetLevel o,
                           ##Final f
                    Where  f.ChildSetName = o.EventSetName and 
                           o.EventSetName = @ObsSetList 
                    Order By o.ChildSetName asc  

                    Insert into ##Final
                    Select *
                    From #Temp1
                    Where  RN = 1
                    Union
                    Select '', '', 
                           '', ChildSetName, RN
                    From #Temp1
                    Where RN > 1


                   Insert Into ##Final
                   Select distinct o.ChildSetName,
                          o.ChildSetName,
                          o.ChildSetName,
                          '',
                          ''
                   From   ##ObsSetLevel o,
                          ##Final f
                   Where f.ChildSetName = o.EventSetName and 
                         o.EventSetName = @ObsSetList     
                   Order By o.ChildSetName asc

                    PRINT @ObsSetList   
                    FETCH NEXT FROM ObsSetList_cursor Into @ObsSetList


            END

Change you select to look like this:

Select o.EventSetName,
        o.EventSetDisplay,
        o.EventSetDescription,
        o.ChildSetName,
        ROW_NUMBER() Over (Order By o.ChildSetName) RN
into #Temp1                 
From   ##ObsSetLevel o,
       ##Final f
Where  f.ChildSetName = o.EventSetName and 
        o.EventSetName = @ObsSetList 
Order By o.ChildSetName asc

You cannot use Insert Into unless you have defined the table or it already exists.

You are not creating #Temp anywhere.

  • Drop the table if it is found.
  • Create the table with the first SELECT .

Try the following:

While @@Fetch_Status = 0 -- For each ObsSetCode - 
BEGIN           

    PRINT 'I am processing the SECTION ------ ' + @dl_EventSetName
    PRINT 'I am processing the ObsSetCode ------ ' + @ObsSetList

    IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL
    BEGIN
        DROP TABLE #Temp1  /* DROP THE TABLE IF IT IS FOUND */
    END

        Select o.EventSetName,
               o.EventSetDisplay,
               o.EventSetDescription,
               o.ChildSetName,
               ROW_NUMBER() Over (Order By o.ChildSetName) RN                  
        Into   #Temp1 /* Create the table with your SELECT */
        From   ##ObsSetLevel o,
               ##Final f
        Where  f.ChildSetName = o.EventSetName and 
               o.EventSetName = @ObsSetList 
        Order By o.ChildSetName asc  
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM