Solved

SQL Reporting - Dynamic Column Names Based on Report Values

Posted on 2012-04-11
6
187 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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