case 不能这样用
必须改语句

解决方案 »

  1.   

    select *  
    from Urge join OweDom_Sub on Urge.lSubID=OweDom_Sub.lID join RentContract on OweDom_Sub.sCntID=RentContract.sContractID
    where Urge.lMID =0
    union 
    select * 
    from Urge join OweDom on Urge.lMID=OweDom.lID join RentContract on OweDom.sCntID=RentContract.sContractID
    where urge.lMID<>0
      

  2.   


    select * from Urge A join OweDom_Sub B on A.lSubID=B.lID 
        join RentContract C on B.sCntID=C.sContractID where A.lMID=0
    union all
    select *  
    from Urge D join OweDom E on D.lMID=E.lID join RentContract F on E.sCntID=F.sContractID
    where D.lMID<>0
      

  3.   

    select *  
    from Urge  join 
    OweDom_Sub on Urge.lSubID=OweDom_Sub.lID and Urge.lMID=0
    join RentContract on OweDom_Sub.sCntID=RentContract.sContractID
    union all
    select *  
    from Urge  join 
    OweDom on Urge.lMID=OweDom.lID and Urge.lMID<>0
    join RentContract on OweDom.sCntID=RentContract.sContractID
      

  4.   

    select *
    from Urge join OweDom_Sub on Urge.lSubID=OweDom_Sub.lID and Urge.lMID=0
    join OweDom on Urge.lMID=OweDom.lID where Urge.lMID<>0
    join RentContract on OweDom.sCntID=RentContract.sContractID
      

  5.   

    最笨的方法:把连接语句分出来,再连上去
    declare @sql varchar(500)
    SELECT   @sql =           --分离连接语句
        CASE Urge.lMID 
             when 0 then ' OweDom_Sub on Urge.lSubID=OweDom_Sub.lID join RentContract on OweDom_Sub.sCntID=RentContract.sContractID'
             else 'OweDom on Urge.lMID=OweDom.lID join RentContract on OweDom.sCntID=RentContract.sContractID'
    end
    set @sql='select * from urage join '+@sql    --加上连接语句
    execute(@sql)                                    --执行语句
      

  6.   

    小马哥,请你讲下你的思路可以吗?
    我用union时,提示  :
    UNION 运算符的 SQL 语句中的所有查询都必须在目标列表中具有相同数目的表达式。