A community member has associated this post with a similar question:
Pivot query result (column name will change)

Only moderators can edit this content.

Pivot query result (column name will change)

RJ 166 Reputation points
2024-10-02T23:11:44.2633333+00:00

Hi there,

Working on a huge db task. A small part of it is pivoting a query result with dynamic column names.

Example Query:
Select top 1 * from information_schema.columns

Output should be

ColumnName | Value

Thanks

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,634 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Erland Sommarskog 112.7K Reputation points MVP
    2024-10-03T21:04:14.6833333+00:00

    For tips on how to write dynamic pivots you can see here: https://www.sommarskog.se/dynamic_sql.html#pivot Note that this link will bring you right into a longer article dynamic SQL. If you are not acquainted with dynamic SQL, you may be better off starting from the beginning of the article. This task requires dynamic SQL.

    ... as long as you insist on doing it in SQL Server. It may be better to return a relational result set and pivot client-side instead.

    0 comments No comments