无法删除临时表无效的对象名称'#Temp1'错误 - 在sql server中

时间:2013-08-16 19:12:13

标签: sql sql-server tsql

我有以下代码抛出一个无效的对象名'#Temp1'错误....

每次迭代,我都会截断表格(DROP表也没有用)....没有明显的跳出来作为一个问题.....感谢任何想法......

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

2 个答案:

答案 0 :(得分:1)

更改您选择看起来像这样:

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

除非已定义表格或已存在,否则无法使用Insert Into

答案 1 :(得分:1)

您没有在任何地方创建#Temp

  • 如果找到,请删除该表。
  • 使用第一个SELECT
  • 创建表格

尝试以下方法:

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  
...