select *, (case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
where my_shop.mycode = s.mycode ) end) as shopname 
from   my_OtherHistoryAudit as s where s.type is null
and 
case when (len(s.mycode)=0) then     
(
exists( select 1 from my_web_shop where s.re = my_web_shop.shopname and my_web_shop.groupcode = '2' )
)
else 
(
exists(select 1 from my_shop where my_shop.mycode = s.mycode and my_shop.companycode= 'companycode' )
)
end
出错

消息 156,级别 15,状态 1,第 10 行
关键字 'exists' 附近有语法错误。
消息 102,级别 15,状态 1,第 11 行
')' 附近有语法错误。
消息 102,级别 15,状态 1,第 15 行
')' 附近有语法错误。

解决方案 »

  1.   

    select *, (case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
    where my_shop.mycode = s.mycode ) end) as shopname 
    from   my_OtherHistoryAudit as s where s.type is null
    and 
    (
    (len(s.mycode)=0 and (select count(*) from my_web_shop where s.re = my_web_shop.shopname and my_web_shop.groupcode = '2')>0) 
    or 
    (len(s.mycode)!=0 and (select count(*) from my_shop where my_shop.mycode = s.mycode and my_shop.companycode= 'companycode')>0)
    )
      

  2.   

    select 
        *,
        (case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
    where my_shop.mycode = s.mycode ) end) as shopname 
    from 
    my_OtherHistoryAudit as s where s.type is null
    and 
    ((len(s.mycode)=0 and exists(select 1 from my_web_shop where s.re = my_web_shop.shopname and my_web_shop.groupcode ='2'))
     or 
     (len(s.mycode)!=0 and exists(select 1 from my_shop where my_shop.mycode = s.mycode and my_shop.companycode= 'companycode')))
      

  3.   

    select *, case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
    where my_shop.mycode = s.mycode ) end as shopname 
    from   my_OtherHistoryAudit s,my_web_shop m
    where s.type is null and len(s.mycode)=0 and s.re = m.shopname and m.groupcode = '2'union allselect *, case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
    where my_shop.mycode = s.mycode ) end as shopname 
    from   my_OtherHistoryAudit s,my_web_shop m
    where s.type is null and len(s.mycode)>0 and m.mycode = s.mycode and m.companycode= 'companycode'case when 只能对值,不能写这样的类似条件了,用union all合并
      

  4.   

    select *, (case when len(mycode)=0 then re else (select top 1 my_shop.shopname from my_shop
    where my_shop.mycode = s.mycode ) end) as shopname 
    from   my_OtherHistoryAudit as s where s.type is null
    and 
        case when (len(s.mycode)=0) then     
        (
            case when exists( select 1 from my_web_shop where s.re = my_web_shop.shopname and my_web_shop.groupcode = '2' ) then 1 else 0 end
        )
        else 
        (
           case when exists(select 1 from my_shop where my_shop.mycode = s.mycode and my_shop.companycode= 'companycode' ) then 1 else 0 end
        )
        end = 1