如下的SQL语句 执行时老报子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的
大家能帮忙看下是什么问题吗
 select distinct InnerCode, b.SecuCode as GPDM,b.CompanyCode as vc_ccode, case left( case isnull(SecuMarket,0)  when 0 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'OH'  end  when 83 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0 )+'OF' else SecuCode+'SH' end  when 90 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'SZ' end  when 73 then SecuCode+'YH'  when 74 then SecuCode+'JY'  when 72 then SecuCode+'HK'  when 71 then SecuCode+'GT'  when 89 then SecuCode+'YH'  when 81 then SecuCode+'QB'  else SecuCode+'OH'  end ,1) when 'Z' then substring(case isnull(SecuMarket,0)  when 0 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF'else SecuCode+'OH'  end  when 83 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'SH' end  when 90 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'SZ' end  when 73 then SecuCode+'YH'  when 74 then SecuCode+'JY'  when 72 then SecuCode+'HK'  when 71 then SecuCode+'GT'  when 89 then SecuCode+'YH'  when 81 then SecuCode+'QB'  else SecuCode+'OH'  end,2,len(SecuCode+'SH')) else case isnull(SecuMarket,0)  when 0 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'OH'  end  when 83 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'SH' end  when 90 then case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0)+'OF' else SecuCode+'SZ' end  when 73 then SecuCode+'YH'  when 74 then SecuCode+'JY'  when 72 then SecuCode+'HK'  when 71 then SecuCode+'GT'  when 89 then SecuCode+'YH'  when 81 then SecuCode+'QB'  else SecuCode+'OH'  end  end as vc_scode,  case left(SecuCode,1) when 'Z' then substring(SecuCode,2,len(SecuCode)) else case SecuCategory when 8 then ( select  c.SecuCode from  LC_CodeChange c  where c.InnerCode=b.InnerCode and c.CodeDefine=8 and isnull(c.StopDate,0)=0) else SecuCode end end as vc_code , case when exists(select 1 from QT_IndexQuote f where f.InnerCode=b.InnerCode) then 1 else 0 end as isIndex  from  SecuMain b where b.SecuMarket in (71,73,81,83,89,90,93) 

解决方案 »

  1.   

    看你这个SQL,头都快晕了。想帮你整理下都累。
      

  2.   

    在你那些case then的子查询中都追加一个where条件:rownum = 1,就应该好用了
      

  3.   

    看到了isnull和是sqlserver的数据库嘛?而且超复杂的case when的语句。整理一下吧,要不太那么这里的一帮兄弟了。
      

  4.   

    看你这个SQL,头都快晕了。想帮你整理下都累。