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

peace
Constraint Violating Yak Guru

404 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
52317 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

404 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

404 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
52317 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  
 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.05 seconds. Powered By: Snitz Forums 2000