标志参数无法正常工作(SQL)

时间:2012-03-13 18:19:28

标签: sql tsql

我有一个“AcctMgr_Flag”,指定一个人作为客户经理。这是在Company_Team表上。如果我将参数@acctmgr发送为'true',我想只返回@member是客户经理的活动。如果@acctmgr不正确,我不关心AcctMgr_Flag是否为真。

SO_Activity中的每个活动都有一个“Assigned_To”列,用于指定member_recid。 Company_Team中的每个成员都有一个AcctMgr_Flag和Company_RecID。 v_rpt_Member中的每个成员都有一个member_recid和一个Company_recid。

这是我的代码

SELECT v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
       SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID,
       v_rpt_ActivityType.SO_Activity_Type_Desc,
       v_rpt_ActivityStatus.SO_Act_Status_Desc
FROM v_rpt_Company 
LEFT OUTER JOIN SO_Activity 
  ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID 
LEFT OUTER JOIN v_rpt_Member 
  ON SO_Activity.Assign_To = v_rpt_Member.Member_ID 
LEFT OUTER JOIN Company_Team 
  ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID AND 
     v_rpt_Company.Company_RecID = Company_Team.Company_RecID 
LEFT OUTER JOIN v_rpt_ActivityType 
  ON SO_Activity.SO_Activity_Type_RecID=v_rpt_ActivityType.SO_Activity_Type_RecID
LEFT OUTER JOIN v_rpt_ActivityStatus 
  ON SO_Activity.so_act_status_recid = v_rpt_ActivityStatus.SO_Act_Status_RecID
WHERE (Company_Team.AcctMgr_Flag = 
       CASE WHEN @acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END) AND 
      (SO_Activity.Assign_To = @member) AND 
      (v_rpt_ActivityStatus.SO_Act_Status_Desc = 
       CASE WHEN @act_status IS NULL 
            THEN v_rpt_ActivityStatus.so_act_status_desc ELSE @act_status END) AND 
      (v_rpt_Company.Company_RecID = 
       CASE WHEN @company = '' THEN v_rpt_Company.Company_RecID ELSE @company END) AND 
      (SO_Activity.Last_Update >= CONVERT(datetime, @date_start, 101)) AND
      (SO_Activity.Last_Update <= CONVERT(datetime, @date_end, 101))
GROUP BY v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
         SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID, 
         v_rpt_ActivityType.SO_Activity_Type_Desc, 
         v_rpt_ActivityStatus.SO_Act_Status_Desc
ORDER BY v_rpt_Company.Company_Name, SO_Activity.Last_Update DESC

1 个答案:

答案 0 :(得分:2)

而不是

(Company_Team.AcctMgr_Flag = CASE WHEN @acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END)

把这个

(@acctmgr != 'true' or Company_Team.AcctMgr_Flag = 1)