在Sql Server中使用Case语句左连接获取ERROR

时间:2014-05-19 11:33:59

标签: sql sql-server

SELECT 
    Camp.ID, Inst.StartDate, Ctype.CampaignType,
    'Cert' + REPLACE(Ctype.CampaignType, ' ', '') AS CampaignType 
FROM 
    CertCampaign Camp
LEFT JOIN
    (SELECT
        CASE WHEN CampaignType = 'CertAccounttoRole' THEN CertUserToAccount) t ON Inst.ID = t.InstanceId
LEFT JOIN  
    CertAccounttoRole ON Inst.ID = t.InstanceId
LEFT JOIN 
    CertCampaignType Ctype ON Ctype.ID = Camp.CampaignTypeId
LEFT JOIN 
    CertInstance Inst ON Inst.CampaignId = Camp.ID

获取错误:

  

Msg 102,Level 15,State 1,Line 6
  ')'附近的语法不正确。

1 个答案:

答案 0 :(得分:1)

您需要在案例stmt中使用END,您还必须为该列命名:

LEFT JOIN(select case when CampaignType='CertAccounttoRole' 
                  then CertUserToAccount
             end as InstanceId )t  ON
Inst.ID=t.InstanceId