Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi guys,

I want to create a procedure for searching purposes.

It has 4 parameters, like name, address, salary and post.
The user can also search on the base of a single parameter or two or three parameters.
This means user should be able to accept null parameters.

How should I do this.

Thanks in advance
Posted
Updated 7-Apr-11 22:02pm
v2
Comments
Dalek Dave 8-Apr-11 4:03am    
Edited for Readabiliy.

Use TSQL's IF statement to check if a parameter is NULL: http://msdn.microsoft.com/en-us/library/ms187471.aspx[^]

IF @param1 IS NULL
BEGIN
    ...
END

...


and construct your SQL accordingly.
As an alternative you may choose to use default values for the parameter definitions of your stored procedure: http://forums.aspfree.com/development-articles-42/using-default-parameter-values-for-t-sql-stored-procedures-40921.html[^].

Happy coding!
 
Share this answer
 
v3
Comments
Sandeep Mewara 8-Apr-11 3:30am    
My 5!
Dalek Dave 8-Apr-11 4:03am    
Good Answer.
Wendelius 8-Apr-11 13:11pm    
Nice :)
create dynamic query like that

and pass one parameter to database

C#
if (InterviewCategoryId == 0)
           {
               if (DateTo == "" && SearchByName == "" && DateFrom != "")
               {
                   Query += " ";
               }
               else if (DateTo != "" && DateFrom == "" && SearchByName == "")
               {
                   Query += " ";
               }
               else if (DateTo == "" && DateFrom == "" && SearchByName != "")
               {
                   Query += " ";
               }
               else if (DateTo != "" && DateFrom != "" && SearchByName != "")
               {
                   Query += "";
               }
               else
               {
                   Query += "";
               }


pass it to the DataBase as one Parameter;

Obj.fn_GetSearcgCandidatesResult(Query);
 
Share this answer
 
in store procedure declare youe variable like this and if your integer is null send it as null don't send it as zero otherwise you have to add another condition in sqlserver.

@parametre1   int=null,
@parametre2   varchar(50)=null,
@parametre3   varchar(50)==null,
@parametre4   varchar(50)==null


then check if parametre is null or not and write your respective query
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900