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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 column name with variable
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

peace
Constraint Violating Yak Guru

420 Posts

Posted - 01/31/2013 :  01:33:42  Show Profile  Reply with Quote
declare @currency nvarchar(3)
set @currency = 'USD'

select currency from tableA

how can i name the currency column based on currency selected?

currencyUSD
80
100
40
.
.
.

if let say set @currency = 'AUS'
currencyAUS
80
100
40
.
.
.

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/31/2013 :  01:45:15  Show Profile  Reply with Quote
you need dynamic sql for that

EXEC('select currency AS currency'+@currency + ' from tableA')

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

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 01/31/2013 :  02:11:36  Show Profile  Reply with Quote
Understood.

Thanks!
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 01/31/2013 :  04:02:38  Show Profile  Reply with Quote
what if to add more column? I tried this but error? need to exec new one?

EXEC('select currency AS currency'+@currency +, totalIn+@currency ' from tableA')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/31/2013 :  04:13:11  Show Profile  Reply with Quote
it should be
EXEC('select currency AS currency'+@currency +', totalIn AS totalIn'+@currency + ' from tableA')

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

Go to Top of Page
  Previous Topic Topic Next 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.14 seconds. Powered By: Snitz Forums 2000