select t1.id,t1.name,t2.name,t2.xtype,
case when t2.xtype=56 or t2.xtype=61 then t3.name
else t3.name+'('+convert(varchar,t2.length)+')' end,
case when t2.isnullable=1 then ''
else 'Y' end
from sysobjects t1,syscolumns t2,systypes t3
where t1.xtype='U'and t1.id=t2.id
and t2.xtype=t3.xtype
--and t1.name='CompanyInfo'
order by t1.id,t2.colid
case when t2.xtype=56 or t2.xtype=61 then t3.name
else t3.name+'('+convert(varchar,t2.length)+')' end,
case when t2.isnullable=1 then ''
else 'Y' end
from sysobjects t1,syscolumns t2,systypes t3
where t1.xtype='U'and t1.id=t2.id
and t2.xtype=t3.xtype
--and t1.name='CompanyInfo'
order by t1.id,t2.colid
本文介绍了一种复杂的SQL查询技巧,通过连接多个表并使用CASE WHEN语句来动态生成列名,从而实现灵活的数据展示。此方法适用于需要根据不同条件展示不同列名的情况。

961

被折叠的 条评论
为什么被折叠?



