SQL 语句-列名与字符串的转换

两个TABLE.请问用SQL实现从TABLE1得到TABLE2这样的结果?
TABLE1:
ROLE P1 P2

A Y
B Y
C Y

TABLE2:

POSITON ROLE
P1 A
P1 B
P2 C
set @sql1='insert into role (role,position) select role,'''+@name+''' FROM TEST where '+@name+' is not null'
--select * from test where p1 is not null
exec(@sql1)
set @i=@i+1
end

问题已经解决..哎,都怪自己太笨,1个字符串与列名的在转换没搞清楚..谢谢大家的回答.

select position = CASE WHEN p1='Y' THEN 'P1'
WHEN p2='Y' THEN 'P2'
ELSE '' END,
role
from table1
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-09-21
select role,(select case p1 when 'Y' then 'P1' else 'P2' end) as position from t_P
第2个回答  2009-09-21
金金金多
相似回答
大家正在搜