遇到困难使用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
)
感谢您的建议。会试试看。 – user2859242
我只是给你举个例子。你可以改变。 – NEER