关于Case语句,请高手指点,急急急!!!

mycenter1117 2003-10-17 10:23:52
有四张表Urge、OweDom_Sub、OweDom、RentContract,主要想实现当Urge.lMID=0时需要join OweDom_Sub,当Urge.lMID<>0时需要join OweDom,下面是我写的SQL语句,可是总是提示"在关键字 'case' 附近有语法错误。 ",不知道为什么?

select *
from Urge join
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
...全文
66 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
vileboy 2003-10-17
  • 打赏
  • 举报
回复
小马哥,请你讲下你的思路可以吗?
我用union时,提示 :
UNION 运算符的 SQL 语句中的所有查询都必须在目标列表中具有相同数目的表达式。
vileboy 2003-10-17
  • 打赏
  • 举报
回复
最笨的方法:把连接语句分出来,再连上去
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) --执行语句
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
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
aierong 2003-10-17
  • 打赏
  • 举报
回复
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
yujohny 2003-10-17
  • 打赏
  • 举报
回复

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
yoki 2003-10-17
  • 打赏
  • 举报
回复
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
yujohny 2003-10-17
  • 打赏
  • 举报
回复
case 不能这样用
必须改语句

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧