我们的采购部门希望我修改我们的应用程序,以便使用部件号搜索申请单请求。
每个申请单请求至少包含一个部分,但可能包含多个部分。
设置了存储过程,以便可以填充字段,也可以不填写字段,因此它会根据传入的内容提取数据。
以下是存储过程在修改之前的工作原理:
ALTER PROCEDURE [dbo].[GetRequisitions](@status_id int, @startdate varchar(50), @enddate varchar(50),
@Vendor_Name varchar(200), @PO_Number varchar(50), @Req_ID int, @datesearch bit, @Part_ID varchar(50)) AS
BEGIN
SET NOCOUNT ON;
select
a.Req_ID, a.Vendor_ID, a.Vendor_Name, a.Req_Date, a.PO_Number, a.Requested_Buy, a.approved,
b.status,
c.supervisor_login,
case when d.req_id is not null then 1 else 0 end as HasDocs
from Req as a
join Status as b on (a.Status_ID=b.Status_ID)
left outer join Supervisor as c on (a.Requested_Login=c.Emp_Login)
left outer join req_doc as d on (a.Req_ID=d.req_id)
where
((@datesearch is null or @datesearch=0) or (a.Req_Date between @startdate and @enddate))
and ((@status_id is null or @status_id=0) or (a.Status_ID=@status_id))
and ((@Vendor_Name is null or LEN(RTrim(@Vendor_Name))=0) or (a.Vendor_Name like '%'+@Vendor_Name+'%'))
and ((@PO_Number is null or LEN(RTrim(@PO_Number))=0) or (a.PO_Number like @PO_Number+'%'))
and ((@Req_ID is null or @Req_ID=0) or (a.Req_ID=@Req_ID))
END
要使用可选的部件号 Part_ID
字段查询此相同的 DataSet ,我添加了以下与Req_Part
数据表:
ALTER PROCEDURE [dbo].[GetRequisitions](@status_id int, @startdate varchar(50), @enddate varchar(50),
@Vendor_Name varchar(200), @PO_Number varchar(50), @Req_ID int, @datesearch bit, @Part_ID varchar(50)) AS
BEGIN
SET NOCOUNT ON;
select
a.Req_ID, a.Vendor_ID, a.Vendor_Name, a.Req_Date, a.PO_Number, a.Requested_Buy, a.approved,
b.status,
c.supervisor_login,
case when d.req_id is not null then 1 else 0 end as HasDocs
from Req as a
join Status as b on (a.Status_ID=b.Status_ID)
left outer join Supervisor as c on (a.Requested_Login=c.Emp_Login)
left outer join req_doc as d on (a.Req_ID=d.req_id)
join Req_Part e on (a.Req_ID=e.Req_ID)
where
((@datesearch is null or @datesearch=0) or (a.Req_Date between @startdate and @enddate))
and ((@status_id is null or @status_id=0) or (a.Status_ID=@status_id))
and ((@Vendor_Name is null or LEN(RTrim(@Vendor_Name))=0) or (a.Vendor_Name like '%'+@Vendor_Name+'%'))
and ((@PO_Number is null or LEN(RTrim(@PO_Number))=0) or (a.PO_Number like @PO_Number+'%'))
and ((@Req_ID is null or @Req_ID=0) or (a.Req_ID=@Req_ID))
and ((@Part_ID is null or LEN(RTrim(@Part_ID))=0) or (e.Part_ID like '%'+@Part_ID+'%'))
END
而且,作为一个FYI:我没有设计这个数据库!我知道列名Requested_Buy
将正确拼写为Requested_By
这一事实,但数据库已经在生产中。拼错的话真让我烦恼!
答案 0 :(得分:1)
如果我理解正确,您可以像这样更新查询。
select
a.Req_ID, a.Vendor_ID, a.Vendor_Name, a.Req_Date, a.PO_Number, a.Requested_Buy, a.approved,
b.status,
c.supervisor_login,
case when d.req_id is not null then 1 else 0 end as HasDocs
from Req as a
join Status as b on (a.Status_ID=b.Status_ID)
left outer join Supervisor as c on (a.Requested_Login=c.Emp_Login)
left outer join req_doc as d on (a.Req_ID=d.req_id)
where
((@datesearch is null or @datesearch=0) or (a.Req_Date between @startdate and @enddate))
and ((@status_id is null or @status_id=0) or (a.Status_ID=@status_id))
and ((@Vendor_Name is null or LEN(RTrim(@Vendor_Name))=0) or (a.Vendor_Name like '%'+@Vendor_Name+'%'))
and ((@PO_Number is null or LEN(RTrim(@PO_Number))=0) or (a.PO_Number like @PO_Number+'%'))
and ((@Req_ID is null or @Req_ID=0) or (a.Req_ID=@Req_ID))
and ( ISNULL(@Part_ID,0) = 0
OR
EXISTS(SELECT NULL FROM Req_Part e WHERE a.Req_ID=e.Req_ID AND e.Part_ID like '%'+@Part_ID+'%')
)