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 2000 Forums
 Transact-SQL (2000)
 Column name based on variable

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-11-08 : 07:45:02
My query as follow,
declare @SCout varchar(20)
declare @ECout varchar(20)
set @SCout='KT'
set @Ecout='JB'

select TID
,sum(case when Cout = @ECout then Posi else 0 end) as @ECout
,sum(case when Cout = @SCout then Posi else 0 end) as @SCout

from DERCout_200910
group by TID

I got an error -- Incorrect syntax near '@ECout'.

How to fix it? I need column name based on variable

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-08 : 08:14:17
That's only possible with dynamic sql.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-11-08 : 11:28:50
quote:
Originally posted by webfred

That's only possible with dynamic sql.


No, you're never too old to Yak'n'Roll if you're too young to die.



Yes. You right

TQVM
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-09 : 02:23:51
www.sommarskog.se/dynamic_sql.html

Madhivanan

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

- Advertisement -