无效的对象名称“myTempCategoryTable”

时间:2017-09-12 07:18:06

标签: sql sql-server sql-server-2008

当我执行这个存储过程时,许多行都被成功执行,但在某行上它给出了错误“invalid object name myTempCategoryTable”。请找到我收到错误的存储过程的一部分。相同的存储过程适用于其他数据库,但对于一个数据库则不适用。有任何许可问题吗? 请帮忙。

Sql server存储过程:

set  @sql= 'CREATE TABLE myTempCategoryTable(MonthNo int,Months NVARCHAR(200),SESSION_ID NVARCHAR(200),' 

            SELECT @MIN_SNO= MIN(SNO) FROM #TempCategory  WHERE SESSIONID=@SESSIONID 
            SELECT @MAX_SNO= MAX(SNO) FROM #TempCategory  WHERE SESSIONID=@SESSIONID 

            WHILE(@MIN_SNO <= @MAX_SNO )
            BEGIN

                SELECT  @CUR_COLUMN= TextVal FROM  #TempCategory WHERE SESSIONID=@SESSIONID and SNO = @MIN_SNO

                IF(@MIN_SNO <= @MAX_SNO )
                BEGIN
                set  @sql = @sql +  REPLACE (REPLACE(@CUR_COLUMN,'-','_'),'\','') + ' NVARCHAR(200),'
                END
                ELSE
                BEGIN

                set  @sql = @sql +  REPLACE (REPLACE(@CUR_COLUMN,'-','_'),'\','') + ' NVARCHAR(200)'
                END

                SET @MIN_SNO = @MIN_SNO+1
            END

            SET @sql = @sql + ')'

            exec (@sql)

        END

      SElect  B.MonthNo,B.Months,A.SESSIONID,A.TextVal, B.TotalCost
      into #c from #TempCategory A inner JOIN  #Temp  B  ON   A.OCBProcessID=B.OCBProcessID
      order by  B.OCBProcessID

     DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
     DECLARE @ColumnName AS NVARCHAR(MAX)

    SELECT @ColumnName= ISNULL(@ColumnName + ',','')     + PlatformArea
    FROM (select distinct '"'+ case when  PM.OCBProcessDesc='Test Preparation' then 'Test_Preparation'  
    when PM.OCBProcessDesc='Test Excecution' then 'Test_Excecution' 
    when PM.OCBProcessDesc='Test Debug Activities' then 'Test_Debug_Activities' 
    when PM.OCBProcessDesc='Test Reporting' then 'Test_Reporting' end +'"' AS PlatformArea  from  OCBProcessDetails PM) AS Weeks

    print @ColumnName
        DECLARE @table TABLE ( id VARCHAR(50) )
        DECLARE @x INT = 0
        DECLARE @firstcomma INT = 0
        DECLARE @nextcomma INT = 0

        SET @x = LEN(@ColumnName) - LEN(REPLACE(@ColumnName, ',', '')) + 1 -- number of ids in id_list

        WHILE @x > 0
        BEGIN
            SET @nextcomma = CASE WHEN CHARINDEX(',', @ColumnName, @firstcomma + 1) = 0
                                    THEN LEN(@ColumnName) + 1
                                    ELSE CHARINDEX(',', @ColumnName, @firstcomma + 1)
                                END
            INSERT  INTO @table
            VALUES  ( SUBSTRING(@ColumnName, @firstcomma + 1, (@nextcomma - @firstcomma) - 1) )
            SET @firstcomma = CHARINDEX(',', @ColumnName, @firstcomma + 1)
            SET @x = @x - 1
        END

    SET @DynamicPivotQuery = 

    N'INSERT INTO myTempCategoryTable
    SELECT  *  FROM   #c 
    PIVOT
    (SUM(TotalCost)
    FOR TextVal IN ('+@ColumnName+')) AS P'

    print @DynamicPivotQuery
    EXEC sp_executesql @DynamicPivotQuery

0 个答案:

没有答案