Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:07:13+00:00 2026-06-16T05:07:13+00:00

I’m getting this error: Must declare the scalar variable @TempTable1. The query works if

  • 0

I’m getting this error:
Must declare the scalar variable “@TempTable1”.
The query works if i remove single quote from around @Temptable1 but no any rows returned.

    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 */    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:07:15+00:00Added an answer on June 16, 2026 at 5:07 am

    So you are declaring a table variable, and trying to run dynamic SQL against it. The problem is that dynamic SQL done with exec or sp_executeSql basically runs in its own scope, so the table variable is not declared there, and, subsequently cannot be used in a query.

    I can propose two workarounds:

    • Make the table available as a temporary table. Because the temp tables are real tables, they are accessible in any scope, so they will be accessible in the dynamic sql. Some pseudo-tsql would be like:

      ...
      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...'
      
    • Pass the table variable to the dynamic sql block as a table valued parameted. This will only work in sql server 2008 and later. This however requires the structure of the table to be defined as a sql server type. This can be done once, on a system level, like:

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

      Then we can use that type to declare the table variable, and also, to pass it like a parameter to the dynamic sql

      ...
      declare @TempTable1 AS TempTableType 
      ...
      @sql = N'... AS PIVOT_CODE FROM @TempTable1) SELECT CourseTitel...'
      EXEC sp_executesql @SQL, 
                         N'@TempTable1 TempTableType READONLY', 
                         @TempTable1 =@TempTable1 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.