Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Parameter as Column Name

Author  Topic 

ganny
Yak Posting Veteran

51 Posts

Posted - 2008-07-31 : 08:15:57
Hi,

I would like to use the parameter as my column name in my report. Please find the below example what am expecting.

1. Created a precedure with the parameters:
CREATE PROCEDURE ESLsp_ServiceOrder
@Month1 varchar(10),
@Month2 varchar(10),
@Month3 varchar(10)

2. passed with the below mentioned values into the parameters:
ESLsp_ServiceOrder 'May','Jun','Jul'

3. i have written the select query as below to show parameter values as my column names:

SELECT NAME, ABC AS @Month1, XYZ AS @Month2, MNO AS @Month3 FROM TEMPTABLE.

Output should show the column names as below:
NAME MAY JUN JUL
--------------------
a 10 20 30
b 20 10 10

But i am getting an error while execute the above query as Incorrect Syntax near @Month1,@Month2,@Month3..

Can anyone please assist me the correct syntax to show the above output.

VGuyz
Posting Yak Master

121 Posts

Posted - 2008-07-31 : 08:25:42
Have you tried u'r sp in dynamic form like below,


declare @Month1 varchar(100),@Month2 varchar(100),@Month3 varchar(100)
set @Month1 ='May'
set @Month2 ='Jun'
set @Month3 ='Jul'
exec('SELECT NAME, ABC AS '+@Month1+', XYZ AS '+@Month2+', MNO AS '+@Month3+' FROM TEMPTABLE')

Go to Top of Page
   

- Advertisement -