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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:31:14+00:00 2026-06-14T12:31:14+00:00

I have successfully constructed the output that I have been looking for from using

  • 0

I have successfully constructed the output that I have been looking for from using dynamic SQL to create a pivot table with dynamically created column names.

My code is:

IF OBJECT_ID('tempdb..#TempDB') IS NOT NULL
DROP TABLE #TempDB
SELECT CASEID, FORMNAME, NAME, VALUE INTO #TempDB FROM dbo.EFORM WHERE FORMNAME='IncidentReporting'

 IF OBJECT_ID('tempdb..#TempDB1') IS NOT NULL
DROP TABLE #TempDB1
SELECT DISTINCT Name INTO #TempDB1 FROM #TempDB

DECLARE @columns varchar(max)
DECLARE @query varchar(max)

SELECT @columns = COALESCE(@columns + ',[' + cast([Name] as varchar(100)) + ']', 
             '[' + cast([Name] as varchar(100))+ ']') 
             FROM #TempDB1

SET @query = 'SELECT * FROM #TempDB AS PivotData '
SET @query = @query  + 
'PIVOT (MAX(VALUE) FOR [NAME] IN (' + @columns + ')) AS p'

EXEC (@query)

This successfully gives me results like:

CASEID  FORMNAME             Column1    Column2 Column3
501000000621    IncidentReporting   Value1  Valuea  Valuev
501000000622    IncidentReporting   Value2  Valueb  Valuew
601000000126    IncidentReporting   Value3  Valuec  Valuex
601000000127    IncidentReporting   Value4  Valued  Valuey
601000000128    IncidentReporting   Value5  Valuee  Valuez

These results, outputed from the @query variable, are in exactly the format that I want a table of these results to be in.

Can anyone tell me how to get the results that are in the @query variable into a standard SQL table?

I have tried doing a statement like this, but I get the message “Incorrect syntax near ‘ + @columns + ‘”:

SELECT *
INTO #TempDB4
FROM (SELECT * FROM #TempDB AS PivotData 
PIVOT (MAX(VALUE) FOR [NAME] IN (' + @columns + ')) AS p)

Many thanks in advance.

  • 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-14T12:31:15+00:00Added an answer on June 14, 2026 at 12:31 pm

    In your existing code, add your into to this line:

    SET @query = 'SELECT * FROM #TempDB AS PivotData '
    

    so that you get:

    SET @query = 'SELECT * INTO #TempDB4 FROM #TempDB AS PivotData '
    

    Or add insert in the same manner.

    To get your unsuccessful query to work as you expect, you’d have to turn that into dynamic SQL, much like your successful query, and call it using exec or sp_executesql

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

Sidebar

Related Questions

I have successfully been using the eclipse Indigo internal browser to view my web
I have constructed several web services that successfully serialize my .NET types into JSON.
I have a windows service that I have successfully installed using installutil but when
I have successfully created a feature in sharepoint that modifies the existing edit dialog
I have successfully migrated all my databases from 2008 to 2012 and mapped the
I have successfully created a custom Facebook open graph action, and have been able
I have successfully constructed something similar to the following code in visual studio 2008:
I have successfully setup castle windsor using an xml configuration file and everything works
I have constructed a PHP file which scrapes a web page (using cURL) to
I'm building a data entry interface and have successfully bound the columns that have

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.