GordonPrince
asked on
best way to write stored procedure with option parameter for where clause
What's the best way to create a stored procedure in SQL 2005 so it will run whether or not a parameter is passed in to it? See sample code.
Sometimes I want to run the select statement with no where clause. But if I pass in a parameter when I execute the stored procedure, then I want the where clause applied.
I thought of doing
"where col1 like %@param1%", is that a good approach? If I set the default value of the variable to be null or something like that?
Any suggestions?
Sometimes I want to run the select statement with no where clause. But if I pass in a parameter when I execute the stored procedure, then I want the where clause applied.
I thought of doing
"where col1 like %@param1%", is that a good approach? If I set the default value of the variable to be null or something like that?
Any suggestions?
create procedure MySP
as
(if I pass in a parameter)
select * from tab1 where col1 = @param1
(if I don't pass in a parameter)
select * from tab1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very elegant. Thanks.
Open in new window