基于两个条件的文本框和下拉列表进行搜索?

时间:2013-10-25 05:33:55

标签: sql-server

    alter PROCEDURE K_HM_SearchFarmeragentDet
@Type varchar(50),@farmername varchar(50)   
AS
BEGIN

    SET NOCOUNT ON;
SELECT MHA.sno,MHA.farmername,MHA.referencedby,HMB.Branch,MHA.area,MHA.contactno,MHA.Type from k_Master_AddHatchingFarmer MHA inner join
 K_HM_Branch HMB on MHA.Branch=HMB.Sno where farmername like @farmername+'%'  order by farmername

END
GO

我已经编写了这样的程序,用于根据农民名称搜索数据,但我想根据农民名称和类型的一个下拉列表搜索数据。 type包含两个值farmer和agent。如果我只想搜索农民,我该如何编写程序请帮助我...

2 个答案:

答案 0 :(得分:2)

试试这个,如果你没有从下拉列表中选择任何内容,请确保将null值传递给存储过程

ALTER PROCEDURE K_HM_SearchFarmeragentDet
@Type varchar(50),@farmername varchar(50)   
AS
BEGIN
SET NOCOUNT ON;
SELECT MHA.sno,MHA.farmername,MHA.referencedby,HMB.Branch,MHA.area,MHA.contactno,MHA.Type from k_Master_AddHatchingFarmer MHA inner join
 K_HM_Branch HMB on MHA.Branch=HMB.Sno 
where farmername like @farmername+'%'  
AND MHA.Type = ISNULL(@Type, MHA.Type)
order by farmername
END
GO

答案 1 :(得分:2)

  alter PROCEDURE K_HM_SearchFarmeragentDet
  @Type varchar(50),@farmername varchar(50)   
  AS
  BEGIN

  SET NOCOUNT ON;
  SELECT MHA.sno,MHA.farmername,MHA.referencedby,HMB.Branch,MHA.area,MHA.contactno,MHA.Type from k_Master_AddHatchingFarmer MHA inner join
  K_HM_Branch HMB on MHA.Branch=HMB.Sno where farmername like @farmername+'%' and    Type=@Type order by farmername

  END
  GO

试试这个