Solved

SQL Reporting - Dynamic Column Names Based on Report Values

Posted on 2012-04-11
6
185 Views
Last Modified: 2012-05-21
I have a report that uses several CTEs, each CTE return several values.  One of the values returned from each CTE is actually one that I need to use as a column name in the final report.  Please can you advise if there is a way assign the value to the column name e.g.

CTE1.Value AS CTE1.ColValue

Obviously wont work but in effect what I am trying to achieve.

Thanks
0
Comment
Question by:Damozz
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 37836072
Are you trying to do this in SSRS, or just name the column in the query results?

If it's SSRS, you should be able to just drop the value onto the report in the header instead of specifying the column name manually, and it should use the first value.

If you're trying to do it in SQL query, you'll need to use dynamic sql to do this, and I'd consider it dangerous, since you'll be injecting the contents of a column into the sql query itself, and a malformed value could have undesired (or damaging) effects, ala Bobby Tables. That said, you'd accomplish it like this:

DECLARE @ColumnName NVARCHAR(100),
        @SQL NVARCHAR(4000)

-- Get your results
SELECT Column1, Column2, Column3, Column4
  INTO #Results
  FROM SomeTableOrSetOfTables

-- Set your column name to the first row of whatever column
SELECT TOP 1 @ColumnName = Column4
  FROM #Results

-- return the results with a re-headered column
SET @SQL = 'SELECT Column1, Column2, Column3, Column4 AS ''' + @ColumnName + ''' FROM #Results'
exec sp_executeSQL @SQL

Open in new window


Is that what you're looking to do?
0
 

Author Comment

by:Damozz
ID: 37836282
I'll porvide a little more detail, I have a report that runs over a 12 month period and the months run April to March, but I need to append the relevant year to the month.  Due to the financial year spanning two calendar years I was looking to use the results to determine the year.
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 37836289
You're still using the term "report" a little unclearly - do you mean just a SQL query, or is the data fed into an SSRS or Crystal report for display? Where do you want to make the correction - in the source query or on the displayed report?

Will my solution work for changing your column name in the source?
0
 

Author Comment

by:Damozz
ID: 37992457
I've requested that this question be deleted for the following reason:

This is no longer a requirement.
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 37992458
I believe the solution will work for the problem as stated - though it's "no longer a requirement", has the OP had a chance to test it? I posted it almost a month ago.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now