views:

2614

answers:

2

select (Case Remark01 When 'l1' then type1 when 'l2' then type2 end) AS [?]--Remark .....want to switch name in here from mytable

Example .... select (Case level When 'l1' then type1 ('l1' mean check constant string) when 'l2' then type2(('l2' mean check constant string)) end) AS (Case when 'l1' then [type01] Else [type02]) from mytable

select level,type1,type2 from mytable

I using two program this mytable one program is want to show menu only type1 only one program is want to show menu only type2 only I using one view using two program..

A: 

You want to dynamically name the column per row? Unfortunately, this can't be done. However, if you have exactly one row then you could use dynamic SQL EXEC (@sqlstring)

But then how will your client know what column name to expect?

Outside of dynamic SQL or for more than one row, then you could pass the name out as another column...

select
    Case level
      When 'l1' then type1
      when 'l2' then type2
   end AS columnvalue,
    Case level
      When 'l1' then type1
      when 'l2' then type2
   end AS outputcolumnname,
from
   mytable
gbn
when this statement excute we can get two column...I need one column...because of ...my program using to table column nameI can get table data value....but I can't get table column name...that is my problem
You can't change the column name dynamically
gbn
A: 

It doesnt make sense to switch the name of a column per row. The rows themselves dont have their own column names, the containing dataset does.

If on the other hand you need to change the name of the column based on data that is input then just selecting the data twice under two names would be easiest. EG:

SELECT  col1 AS alias1,
        col1 AS alias2
FROM    myTable
Jack Ryan