stored procedure to search from a table with several condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amit Rai
    New Member
    • Aug 2010
    • 5

    stored procedure to search from a table with several condition

    suppose there are 3 textboxes and 1 search button.If we click on search button without filling any textfield, then
    it display all data from the table.when we fill any field, then search according to the number of fields being filled.
    here is the sp

    create proc sp_search
    @name varchar(10), @city varchar(10),@ad r varchar(10),que ry varchar(100)
    as
    begin
    @query="select *from employee where 1=1";
    if(@name!=null)
    @query=@query+@ name;
    if(@city !=null)
    @query=@query+@ city;
    if(@adr!=null)
    @query=@query+@ city;
    end
  • gpl
    New Member
    • Jul 2007
    • 152

    #2
    This will always return all rows, as your first condition is
    where 1=1
    no matter what other conditions you add, 1 will always be 1

    Comment

    Working...