日期:2014-05-17  浏览次数:20649 次

这两个case when 语句为什么有错
1。
select 
  case @SamplingStepDateValue when 'A' then * 
  else top 1 * 
  end
From @tbl 

2。
。。。。。(已经选好内容,需要排序)
ORDER BY 
  case @SamplingStepDateValue ='A'then
RemediationId
, TYear_seq DESC
, WTHRGH_Seq DESC
, CONST_Seq DESC
, RECD_Seq DESC
, CLEAN_Seq DESC
, REF_Seq DESC
  case @SamplingStepDateValue ='E' then
  DATESAM_DATE ASC
  case @SamplingStepDateValue ='L' then
  DATESAM_DATE DESC
 

------解决方案--------------------
select
case @SamplingStepDateValue when 'A' then *
else top 1 *
end
From @tbl
-->不能用*,要具体一列
------解决方案--------------------
case when then else end 相当于三目运算,只能是单个值,不能是数据集
------解决方案--------------------
你的第二个是语法问题,你可以看看,第一个也是我前两天正在纠结的问题,然后我换个写法,用if exists来写的,这里我总算明白了,为什么,我的当时写法错了,因为我查询的结果是一个数据集
------解决方案--------------------
一个查询中order by 比较的字段或表达式只能是固定的,不能用一个case 来order by 不固定的字段.