SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecte statement with the parameter as the header
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sindhu sarah
Starting Member

United Arab Emirates
34 Posts

Posted - 11/28/2011 :  05:42:13  Show Profile  Reply with Quote
Hi all,

i need a solution for the following query.
declare @r1 as varchar (5)
set @r1='Title'
select name as @r1 from customer

i wanted the solution to be as
Title
Samp1
Samp2
Samp3

But when io execute the above statement it displays with
@r1
Samp1
Samp2
Samp3

The above solution needs to be made in an union all query with a diffrent sql statement.Please let me know how to retrieve the header value from variable and display it with the other column.Hope its clear or else please let know.I can explain in more depth.
Thanks in advance.

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3383 Posts

Posted - 11/28/2011 :  05:48:12  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
declare @sql varchar(8000)
select @sql = 'select name as [' + @r1 + '] from customer'

exec (@sql)

This has security implications though.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22772 Posts

Posted - 11/28/2011 :  07:07:14  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by sindhu sarah

Hi all,

i need a solution for the following query.
declare @r1 as varchar (5)
set @r1='Title'
select name as @r1 from customer

i wanted the solution to be as
Title
Samp1
Samp2
Samp3

But when io execute the above statement it displays with
@r1
Samp1
Samp2
Samp3

The above solution needs to be made in an union all query with a diffrent sql statement.Please let me know how to retrieve the header value from variable and display it with the other column.Hope its clear or else please let know.I can explain in more depth.
Thanks in advance.




The above code is not valid statement becuase you cannot use variable as alias name. Noty sure why you did not get any error.
Also make sure to read this
www.sommarskog.se/dynamic_sql.html

Madhivanan

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

sindhu sarah
Starting Member

United Arab Emirates
34 Posts

Posted - 11/28/2011 :  07:43:05  Show Profile  Reply with Quote
Thanks for your reply madhivanan.
i got solution.This can be solved by dynamic query.

declare @r1 as varchar (5)
set @r1='Title'
declare @Query1 nvarchar(4000)
exec ('select Dimension6_ as '+ @r1+' from LedgerTrans')

Title
000000
000000
000000
000000
000000
000000
000000

Hope this might be useful.

Go to Top of Page

sindhu sarah
Starting Member

United Arab Emirates
34 Posts

Posted - 11/28/2011 :  07:44:53  Show Profile  Reply with Quote
Thanks for the solution nigelrivett.Just noticed.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000