2016-07-26 136 views
-1

遇到困難使用CASE聲明與LIKE運算符。在下面的存儲過程中,所有參數都不是必需的,所以當用戶沒有輸入PatientName進行搜索時,下面的查詢不會返回預期的結果,因爲LIKE運算符在外面很明顯。我正在尋找這樣的東西,以便當用戶不輸入PatientName時,它將是與Like運算符

(c.LastName + c.Firstname) = @PatientNAme else 
(c.LastName + c.Firstname) like '%' + @PatientNAme + '%' 

* 

    (c.LastName + c.Firstname) (       
     CASE @PatientName       
      WHEN '' THEN @PatientName = (c.LastName + c.Firstname)       
      ELSE like '%' + @PatientName + '%'    
     END       
    ) 

* 

CREATE proc [dbo].[SearchParkPrescriptionDetails] 
    @DispenseID INT, 
    @ParkPrescriptionReasonId INT, 
    @PrescriptionParkType VARCHAR(50), 
    @PatientName VARCHAR(120), 
    @User VARCHAR(120), 
    @fromdate DATETIME, 
    @todate DATETIME, 
    @DateWiseSearch VARCHAR(3), 
    @PatientID INT 
AS 
BEGIN 
    SELECT 
      a.ParkPrescriptionId 
     , a.DispenseID 
     , a.ParkPrescriptionReasonId 
     , a.ParkDate 
     , (c.LastName + ' ' + c.Firstname) AS PatientName 
     , d.PrescriptionType 
     , e.ParkPrescriptionReason 
     , a.Notes 
     , b.ItemCount AS TotalItems 
     , g.ExemptionReason 
     ,a.[User] 
    FROM 
     ParkPrescriptionDetails a 
     INNER JOIN 
       Dis_DispenseMaster b 
     ON 
       a.DispenseID=b.DispenseID 
     INNER JOIN 
       Patient c 
     ON 
       b.PatientId = c.PatientId 
     INNER JOIN 
       Lookup_PrescriptionType d 
     ON 
       b.PrescriptionTypeId = d.PrescriptionTypeId 
     INNER JOIN 
       Lookup_ParkPrescriptionReason e 
     ON 
       a.ParkPrescriptionReasonId = e.ParkPrescriptionReasonId 
     LEFT JOIN 
       Lookup_ExemptionReason g 
     ON 
       b.ExemptionReasonId = g.ExemptionReasonId 
    WHERE 
     CONVERT(DATE, a.ParkDate) BETWEEN @fromdate AND @todate 
     AND a.RecallStatus    = 'N' 
     AND a.DispenseID    = (CASE @DispenseID WHEN 0 THEN a.DispenseID ELSE @DispenseID END) 
     AND b.PatientId    = (CASE @PatientID WHEN 0 THEN b.PatientId ELSE @PatientID END) 
     AND a.ParkPrescriptionReasonId = (CASE @ParkPrescriptionReasonId WHEN 0 THEN a.ParkPrescriptionReasonId ELSE @ParkPrescriptionReasonId END) 
     AND 
     (
       c.LastName + c.Firstname 
     )      LIKE (CASE @PatientName WHEN '' THEN (c.LastName + c.Firstname) ELSE '%' + @PatientName + '%' END) 
     AND a.[User]   LIKE (CASE @User WHEN '' THEN a.[User] ELSE '%' + @User + '%' END) 
     AND b.ParkPrescription = (CASE @PrescriptionParkType WHEN '' THEN b.ParkPrescription WHEN 'Park' THEN 'Y' END) 
     AND b.RecallPrescription = (CASE @PrescriptionParkType WHEN '' THEN b.RecallPrescription WHEN 'Recall' THEN 'Y' END) 
     AND a.IsDeleted   =0 
END 

==========改變它像這樣。這是完美的===========


(c.LastName + ' ' + c.Firstname) = (       
     CASE @PatientName       
      WHEN '' THEN (c.LastName + ' ' + c.Firstname)       
      ELSE '%' + @PatientName + '%'    
     END       
    ) 

回答

0

不要在where clause使用CASE WHEN。使用OR

(
    @PatientName = '' OR 
    (c.LastName + c.Firstname) LIKE '%' + @PatientName + '%' 
) AND 
(
    @User = '' OR 
    a.[User] LIKE '%' + @User + '%' 
) -- Same for others 

,而不是

(c.LastName + ' ' + c.Firstname) = (       
     CASE @PatientName       
      WHEN '' THEN (c.LastName + ' ' + c.Firstname)       
      ELSE '%' + @PatientName + '%'    
     END       
    ) 
+0

感謝您的建議。會試試看。 – user2859242

+0

我只是給你舉個例子。你可以改變。 – NEER

0

T-SQL有兩種形式的CASE。你正在使用「簡單案例」。嘗試 「搜索案」 - 例如爲:

CASE    
WHEN @PatientName = '' THEN [ return something ] 
WHEN @PatientName like [ pattern ] THEN [ return something ] 
ELSE [ return something else ] 
END 
+1

稱爲 'CASE搜索' VS '簡單的例子' – dfundako

+0

謝謝 - 我的回答包括在此。 –

+0

感謝您的建議將嘗試實施。 – user2859242