我正在编写高级搜索,但该过程有效但返回所有选定的行并且不执行任何检查。
ALTER proc [dbo].[spSearchpatientinfo]
@user nvarchar(50),
@accountstart int,
@accountend int,
@mnumber nvarchar(20),
@namefirst nvarchar(30),
@namelast nvarchar(100),
@registrationfrom datetime,
@registrationto datetime,
@office nvarchar(1),
@status nvarchar(1)
as
begin
select
pi_reg_id, pi_first_name, pi_last_name,
pi_dob, pi_address, pi_city, pi_phone_home,
pi_phone_work, pi_Email, pi_status
from
MData.dbo.[Patient info]
where
(pi_insert_user = @user)
or (@accountstart IS NULL or pi_reg_id >= @accountstart)
or (@accountend IS NULL or pi_reg_id <= @accountend)
or (@mnumber IS NULL or pi_phone_home like @mnumber)
or (@namefirst is null or pi_first_name like @namefirst)
or (@namelast is null or pi_last_name like @namelast)
or (@registrationfrom is null or pi_reg_date >= @registrationfrom)
or (@registrationto is null or pi_reg_date <= @registrationto)
or (@office is null or pi_office = @office)
or (@status is null or pi_status = @status)
end
请告诉我正在做的错误
答案 0 :(得分:0)
由于您通过下级或子句检查传入的变量,我不认为您对外部条款的确是OR。
ALTER proc [dbo].[spSearchpatientinfo]
@user nvarchar(50),
@accountstart int,
@accountend int,
@mnumber nvarchar(20),
@namefirst nvarchar(30),
@namelast nvarchar(100),
@registrationfrom datetime,
@registrationto datetime,
@office nvarchar(1),
@status nvarchar(1)
as
begin
select pi_reg_id,pi_first_name,pi_last_name,pi_dob,pi_address,pi_city,pi_phone_home,pi_phone_work,pi_Email,pi_status from MData.dbo.[Patient info]
where (@user is null or pi_insert_user = @user)
AND (@accountstart IS NULL or pi_reg_id >= @accountstart)
AND (@accountend IS NULL or pi_reg_id <= @accountend)
AND (@mnumber IS NULL or pi_phone_home like @mnumber)
AND (@namefirst is null or pi_first_name like @namefirst)
AND (@namelast is null or pi_last_name like @namelast)
AND (@registrationfrom is null or pi_reg_date >= @registrationfrom)
AND (@registrationto is null or pi_reg_date <= @registrationto)
AND (@office is null or pi_office = @office)
AND (@status is null or pi_status = @status)
end
答案 1 :(得分:0)
虽然我发现了一个潜在的问题,但这里的SP是为了给你所寻找的而改写的。在WHERE子句中,“like”是你的参数,附加了“%”?如果没有,他们可能找不到任何东西。
ALTER PROC [Dbo].[Spsearchpatientinfo]
@user NVARCHAR(50),
@accountstart INT,
@accountend INT,
@mnumber NVARCHAR(20),
@namefirst NVARCHAR(30),
@namelast NVARCHAR(100),
@registrationfrom DATETIME,
@registrationto DATETIME,
@office NVARCHAR(1),
@status NVARCHAR(1)
AS
BEGIN
SELECT Pi_Reg_Id,
Pi_First_Name,
Pi_Last_Name,
Pi_Dob,
Pi_Address,
Pi_City,
Pi_Phone_Home,
Pi_Phone_Work,
Pi_Email,
Pi_Status
FROM Mdata.Dbo.[Patient Info]
WHERE(Pi_Insert_User = @user)
AND Pi_Reg_Id >= ISNULL(@accountstart, Pi_Reg_Id)
AND Pi_Reg_Id <= ISNULL(@accountend, Pi_Reg_Id)
AND Pi_Phone_Home LIKE ISNULL(@mnumber, Pi_Phone_Home)
AND Pi_First_Name LIKE ISNULL(@namefirst, Pi_First_Name)
AND Pi_Last_Name LIKE ISNULL(@namelast, Pi_Last_Name)
AND Pi_Reg_Date >= ISNULL(@registrationfrom, Pi_Reg_Date)
AND Pi_Reg_Date <= ISNULL(@registrationto, Pi_Reg_Date)
AND Pi_Office = ISNULL(@office, Pi_Office)
AND Pi_Status = ISNULL(@status, Pi_Status);
END;