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
 General SQL Server Forums
 New to SQL Server Programming
 column name with variable

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-01-31 : 01:33:42
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

52326 Posts

Posted - 2013-01-31 : 01:45:15
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 - 2013-01-31 : 02:11:36
Understood.

Thanks!
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2013-01-31 : 04:02:38
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

52326 Posts

Posted - 2013-01-31 : 04:13:11
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
   

- Advertisement -