Solved

Is a dynamic column alias possible?

Posted on 2004-03-26
4
5,696 Views
Last Modified: 2011-06-17
Hello,

I run a stored procedure every quarter that has one input parameter, the current date.  

For one data item, values are secured for the current and past 11 quarters.  The table column names for this field are:  Current, Current_Minus1, Current_Minus2…Current_Minus11.  

I would like for my output to have different “quarter names” for the columns instead of the actual column names.  

A hard-coded “AS” Statement works great, for example:
Select Current as 1Q04, Current_Minus1 as 4Q03, Current_Minus2 as 3Q03, etc.
But I do not want to change this every quarter.

Is there a type of variable that can be used instead of the hard-coded values???  I would like to calculate the column names and place them in a variable so that I could code something like:

“Select Current as @CurrentQtr, Current_Minus1 as @CurrentQtrMinus1, Current_Minus2 as @CurrentQtrMinus2, … “

I have attempted this approach using several differnt variable types and always receive an error.  Is there a specific variable type that works or appropriate syntax to make this possible??  If not - Any other ideas that might make this work?

Down the road I will be creating an Access front-end screen that will allow users to create this report themselves for a specific current date by clicking a button that will execute the stored procedure generate a spreadsheet.  If the approach above is not possible in SQL perhaps it would be possible in Access??

Thank you for your assistance!

TXAmmonite (David)
0
Comment
Question by:TXAmmonite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10690391
this is a presentation issue and you should get your GUI / Frontend to
name the columns appropriately...

that should be possible in access


it is possible in SQL Server by using dynamic sql
but i would recommend it's use for this scenario...  

e.g.
declare SQLSTR varchar(8000)

set @sqlstr= “Select Current as @CurrentQtr, Current_Minus1 as @CurrentQtrMinus1, Current_Minus2 as @CurrentQtrMinus2, … “

Exec (@SQLSTR)


hth


0
 

Author Comment

by:TXAmmonite
ID: 10692100
hth,

I wish it were that simple - I had already tried this approach.  
All variables where declared as varchars and populated (selected each to verify)

Created the dynamic SQL as:
set @SQLSTR= 'Select Current as @Curr_Qtr,
Current_Minus1 as @Curr_QDate_Minus1,
Current_Minus2 as @Curr_QDate_Minus2
from Tbl_MyTablel'
Exec (@SQLSTR)

I received the error:

Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near '@Curr_Qtr'.

After removing the first "@" the error would change to
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near '@Curr_QDate_Minus1'.

Removing all "@"s allowed the query to execute.  

Still looking for that special variable type or format.

Thanks,
David
0
 
LVL 42

Accepted Solution

by:
Eugene Z earned 125 total points
ID: 10692639
try this please:

declare @SQLSTR varchar(2000)
declare @Curr_Qtr varchar(50)
declare  @Curr_QDate_Minus1 varchar(50)
declare  @Curr_QDate_Minus2 varchar(50)
set  @Curr_Qtr ='1Q04'
set  @Curr_QDate_Minus1 ='2Q04'
set  @Curr_QDate_Minus2 ='3Q04'
set @SQLSTR= 'Select [Current] as [' + @Curr_Qtr + '],
Current_Minus1 as [' + @Curr_QDate_Minus1 + '],
Current_Minus2 as [' + @Curr_QDate_Minus2 + ']
from Tbl_MyTablel'

--print @SQLSTR
exec(@SQLSTR)
0
 

Author Comment

by:TXAmmonite
ID: 10704412
EugeneZ

Thanks - this worked great!
TXAmmonite
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

740 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