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 6337051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:10:36+00:00 2026-05-24T19:10:36+00:00

I am writing a query to pivoting table elements where column name is generated

  • 0

I am writing a query to pivoting table elements where column name is generated dynamically.

SET @query = N'SELECT STUDENT_ID, ROLL_NO, TITLE, STUDENT_NAME, EXAM_NAME, '+
            @cols +
            ' INTO ##FINAL 
            FROM
            (
                SELECT  *
                FROM #AVERAGES
                UNION 
                SELECT  *
                FROM #MARKS 
                UNION
                SELECT  *
                FROM #GRACEMARKS
                UNION
                SELECT  *
                FROM #TOTAL
                ) p
                PIVOT
                (
                MAX([MARKS])
                FOR SUBJECT_ID IN
                ( '+
                @cols +' )
            ) AS FINAL
            ORDER BY STUDENT_ID ASC, DISPLAYORDER ASC, EXAM_NAME ASC;'

EXECUTE(@query) 

select * from ##FINAL 

This query works properly in my local database, but it doesn’t work in SQL Azure since global temp tables are not allowed there.

Now if i change ##FINAL to #FINAL in my local database, but it gives me error as

Invalid object name ‘#FINAL’ .

How can I resolve this issue?

  • 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-05-24T19:10:37+00:00Added an answer on May 24, 2026 at 7:10 pm

    Okay, after saying I didn’t think it could be done, I might have a way. It’s ugly though. Hopefully, you can play with the below sample and adapt it to your query (without having your schema and data, it’s too tricky for me to attempt to write it):

    declare @cols varchar(max)
    set @cols = 'object_id,schema_id,parent_object_id'
    
    --Create a temp table with the known columns
    create table #Boris (
        ID int IDENTITY(1,1) not null
    )
    --Alter the temp table to add the varying columns. Thankfully, they're all ints.
    --for unknown types, varchar(max) may be more appropriate, and will hopefully convert
    declare @tempcols varchar(max)
    set @tempcols = @cols
    while LEN(@tempcols) > 0
    begin
        declare @col varchar(max)
        set @col = CASE WHEN CHARINDEX(',',@tempcols) > 0 THEN SUBSTRING(@tempcols,1,CHARINDEX(',',@tempcols)-1) ELSE @tempcols END
        set @tempcols = CASE WHEN LEN(@col) = LEN(@tempcols) THEN '' ELSE SUBSTRING(@tempcols,LEN(@col)+2,10000000) END
        declare @sql1 varchar(max)
        set @sql1 = 'alter table #Boris add [' + @col + '] int null'
        exec (@sql1)
    end
    
    declare @sql varchar(max)
    set @sql = 'insert into #Boris (' + @cols + ') select ' + @cols + ' from sys.objects'
    exec (@sql)
    
    select * from #Boris
    
    drop table #Boris
    

    They key is to create the temp table in the outer scope, and then inner scopes (code running within EXEC statements) have access to the same temp table. The above worked on SQL Server 2008, but I don’t have an Azure instance to play with, so not tested there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a query to select data from a table called contest. each
I'm writing a query to get ALL of the products in the products table,
I came across writing the query in differnt ways like shown below Type-I SELECT
I am having trouble writing a query on a mysql table of user activity
Today, I have started writing a query which is as given below: SELECT IP1.InstId,CAST(IP2.Quote
I'm writing a query into a table that keeps track of a salesman's status,
I am writing a query which has multiple select statements in an insert statement
I am writing a SQL query that I would like to add a column
I am writing a query to this effect: select * from players where player_name
I am having trouble writing a query to select one row per date, given

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.