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 a header in select statement

Author  Topic 

sindhu sarah
Starting Member

34 Posts

Posted - 2011-11-28 : 03:41:24
Hi all,

I need to know a solution for the following:

declare @r1 as varchar (5)
set @r1='Title'
select name as @r1 from customer

i need the column records from the customer table with the header 'Title'

ex: Title
samp1
samp2

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-28 : 03:42:50
this is a display issue and should be handled at front end. Why do you have to change sql query for this?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-28 : 03:45:20
if you want to do it in sql, you need to use dynamic sql

declare @r1 as varchar (5),@sql nvarchar(1000),@params nvarchar(500)
set @r1='Title'
set @params ='@r varchar(5)'
set @sql = 'select name as ' + @r1 + ' from customer'
EXEC sp_executesql @sql,@params,@r1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-11-28 : 07:21:54
Duplicate of http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=168430

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-12-27 : 08:09:51
quote:
Originally posted by sindhu sarah

Hi all,

I need to know a solution for the following:

declare @r1 as varchar (5)
set @r1='Title'
select name as @r1 from customer

i need the column records from the customer table with the header 'Title'

ex: Title
samp1
samp2




What did you change after one year of asking this question?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -