Sql server 2008 更正asp.net的存储过程
我正在编写一个高级搜索,该过程可以运行,但返回所有选定的行,并且不执行where检查Sql server 2008 更正asp.net的存储过程,sql-server-2008,Sql Server 2008,我正在编写一个高级搜索,该过程可以运行,但返回所有选定的行,并且不执行where检查 ALTER proc [dbo].[spSearchpatientinfo] @user nvarchar(50), @accountstart int, @accountend int, @mnumber nvarchar(20), @namefirst nvarchar(30), @namelast nvarchar(100), @registrationfrom
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
请告诉我所犯的错误,因为您是在检查通过从属or子句传入的变量是否为null,我认为您不是指外部子句上的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
这里是重新编写的SP,以提供您所需的内容,尽管我看到了一个潜在问题。在WHERE子句中,诸如此类的参数是否随附%?如果没有,他们可能什么也找不到
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;
如果其中任何一个或语句为true,则将返回与该条件匹配的所有结果。因此,如果您只是在寻找一个特定的用户,并且@status为null,那么它将被返回。即使不是为同一个pi_insert_用户。也许你的意思是,而不是所有那些外部or…你是否尝试过不返回任何内容的输入?它现在不会返回任何内容。是@User总是被填写,还是我们也需要检查它是否为null?我想知道传入的值会有所帮助,这样我们可以通过逻辑进行思考,并确保您有符合该条件的记录…@用户始终填充数据,其他所有数据都可以为空,或者根据用户的选择填充一些数据。例如,填充pi_Reg_ID。如果没有记录>=帐户开始和帐户结束,则不应返回任何记录,对吗?因此,让我们关注一个特定的示例,并确保我们有与该示例匹配的记录。这可能有助于理解问题所在,因为就目前而言,只要为@values传入null,那么在我看来它应该可以工作。在outer中使用and将导致跳过所有其他行,这不是必需的。