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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:56:57+00:00 2026-06-14T08:56:57+00:00

I am trying to use dynamic SQL to do a pivot on a table

  • 0

I am trying to use dynamic SQL to do a pivot on a table for which I need to dynamically generate the column names.
My code is:

DECLARE @columns varchar(max)
DECLARE @query varchar(max)
SELECT @columns = COALESCE(@columns + ',[' + cast([Name] as varchar(max)) + ']', 
                 '[' + cast([Name] as varchar(max))+ ']')           
    FROM   dbo.Temp2

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

EXEC (@query)

My @columns function seems to work (though I can only ‘print’ 8000 characters to verify), and I have read that it is acceptable to do a MAX or MIN function on non-numeric varchars in SQL 2005, but when I run the query in its complete form I get the error message:

Msg 321, Level 15, State 1, Line 1
"MAX" is not a recognized table hints option. If it is intended as a parameter to a table-valued function, ensure that your database compatibility mode is set to 90.

I have checked the compatibility level and it is set to 90.
Can anyone offer any suggestion for how to get past this?

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-14T08:56:59+00:00Added an answer on June 14, 2026 at 8:56 am

    You are missing a space between PivotData and PIVOT.

        SET @query = @query  + 
        ' PIVOT (MAX(VALUE) FOR [NAME] IN (' + @columns + ')) AS p' 
    //   ^--- HERE
    

    As the result, the SQL parser interprets PivotDataPIVOT as a single identifier, resulting in a syntax error later on.

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

Sidebar

Related Questions

I am trying to use a strong ref cur with dynamic sql statment but
I am trying to use Dynamic Linq library in my code, but it gives
i'm trying to convert a 'TableController'-Class we used (without ORM) to generate dynamic SQL
I am trying to use some dynamic sql. I have generated over 100 parameters,
I've been trying to use a dynamic view in Kohana 3.2 without success. I'm
i'm trying to create a Dynamic Sub-menu to use it in Wordpress, Something like
I am need paint my image. I'm trying use JQuery in here this link:
I am trying to generate an insert statement from a row in a table?
I am trying to populate a dataset with data from a dynamic SQL dataset
I'm using C# with TSQL and SQL Server 2005 I'm trying to use PetaPoco

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.