1

我收到此错误:必须声明标量变量“@TempTable1”。如果我从 @Temptable1 周围删除单引号但没有返回任何行,则查询有效。

    ALTER PROCEDURE dbo.StoredProcedure2
      @Studentcode int
    AS
      DECLARE @sql AS nvarchar(max)
      DECLARE @pivot_list AS nvarchar(max) -- Leave NULL for COALESCE technique
      DECLARE @select_list AS nvarchar(max) -- Leave NULL for COALESCE technique
      Declare @TempTable1 AS table(CourseTitel nvarchar(50) , Grade nvarchar(50))

      INSERT INTO @TempTable1 (CourseTitel, Grade) 
       SELECT Courses.CourseTitel, Status.Status 
       FROM Status 
         INNER JOIN Courses ON Status.CourseCode = Courses.CourseCode 
       WHERE (Status.StudentCode = @Studentcode)

      SELECT @pivot_list = COALESCE(@pivot_list + ', ','') + '[' + CONVERT(varchar, PIVOT_CODE) + ']',
             @select_list = COALESCE(@select_list + ', ','') + '[' + CONVERT(varchar, PIVOT_CODE) + '] AS [col_' + CONVERT(varchar, PIVOT_CODE) + ']'
      FROM (SELECT DISTINCT PIVOT_CODE 
            FROM (SELECT CourseTitel, Grade, ROW_NUMBER() OVER (PARTITION BY CourseTitel ORDER BY Grade) AS PIVOT_CODE 
                  FROM @TempTable1) 
            AS rows) 
      AS  PIVOT_CODES

    SET @sql = ';WITH p AS (SELECT CourseTitel, Grade, 
                ROW_NUMBER() OVER (PARTITION BY urseTitel ORDER BY Grade) AS PIVOT_CODE 
                FROM ' + @TempTable1 + ' ) SELECT CourseTitel, ' + select_list + '
                FROM p PIVOT (MIN(Grade)FOR PIVOT_CODE IN (' + @pivot_list + ')) AS pvt'
    --PRINT (@sql)
    EXEC (@sql)
    /* SET NOCOUNT ON */    
4

1 回答 1

1

因此,您正在声明一个表变量,并尝试对其运行动态 SQL。问题是动态 SQL 完成execsp_executeSql基本上在其自己的范围内运行,因此表变量没有在那里声明,并且随后不能在查询中使用。

我可以提出两种解决方法:

  • 使该表可用作临时表。因为临时表是真实的表,它们在任何范围内都可以访问,所以它们将在动态 sql 中可以访问。一些伪 tsql 就像:

    ...
    if object_id('tempdb..#TempTable1 ') is not null
      drop table #TempTable1 
    
    create table #TempTable1 (CourseTitel nvarchar(50) , Grade nvarchar(50))
    ...
    @sql = '... AS PIVOT_CODE FROM #TempTable1) SELECT CourseTitel...'
    
  • 将表变量作为表值参数传递给动态 sql 块。这仅适用于 sql server 2008 及更高版本。但是,这需要将表的结构定义为 sql server 类型。这可以在系统级别完成一次,例如:

    CREATE TYPE TempTableType AS TABLE (CourseTitel nvarchar(50) , Grade nvarchar(50))
    

    然后我们可以使用该类型来声明表变量,并将其作为参数传递给动态 sql

    ...
    declare @TempTable1 AS TempTableType 
    ...
    @sql = N'... AS PIVOT_CODE FROM @TempTable1) SELECT CourseTitel...'
    EXEC sp_executesql @SQL, 
                       N'@TempTable1 TempTableType READONLY', 
                       @TempTable1 =@TempTable1 
    
于 2012-12-22T21:26:23.397 回答