一个sql查询语句,在查询列后加条件判断要追加的列 if ... select a,b from tbelse select a,b,c from tb 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select a,b,case when 条件 then c end as cfrom table select a,b,c=case when 条件 then xx when 条件 then xx endfrom tb declare @sql varchar(max)select @sql='select a,b'+case when 条件 then ',c' else '' end+' from table'exec (@sql) 我的意思是,就用一个查询语句,不写声明,和函数,过程等,就一个查询语句,在select查询语句查询列之中写条件例:select 姓名,班级 条件 (条件成立查出 学生编号)否则不查 select a,b,c=case when 条件 then xx when 条件 then xx end from tb select 姓名,班级 ,学生编号=case when 条件成立 then 学生编号 else '' endfrom tb 以上都不对呀,在关键字 'then' 附近有语法错误。 你条件没写对.请不要一尘不变地复制.select 姓名,班级 ,学生编号=case when 分数>60 then 学生编号 else '' endfrom tb 我条件写对着呢,我早改了,出是出来了,就是多了一列,因为是 学生编号=case ..........不管条件成立不成立,都会显示 学生编号列,只不过条件不成立时 学生编号列为null成立时有内容,但都有学生编号列.......清楚吗??不过,还是要谢谢你 触发器问题修正,解决问题立即给分 一道数据复制操作题^^^^^^^^^^^^^^^^^^^^^^^^^ 倒序运行游标 BBS中,查询帖子并按要求排列的SQL语句怎么写 为什么不让我修改表的字段的数据类型。 循环问题,使用游标过慢,有啥好方法? 请教我一个SQL语句~!在线等待~ 一个挺郁闷的排序问题 加急的 关于删除的问题!帮帮忙! 急:浏览器与后台数据库的检索问题 一个触发器的写法? 问一个字符串比较问题
from table
when 条件 then xx end
from tb
select @sql='select a,b'+case when 条件 then ',c' else '' end+' from table'
exec (@sql)
例:select 姓名,班级 条件 (条件成立查出 学生编号)否则不查
when 条件 then xx end
from tb
,学生编号=case
when 条件成立 then 学生编号
else ''
end
from tb
请不要一尘不变地复制.select 姓名,班级
,学生编号=case
when 分数>60 then 学生编号
else ''
end
from tb
不管条件成立不成立,都会显示 学生编号列,只不过条件不成立时 学生编号列为null
成立时有内容,但都有学生编号列.......清楚吗??
不过,还是要谢谢你