sql server programming:
rodchar
If you know which columns, just allow optional input parameters (passed
as NULL):
CREATE PROCEDURE dbo.MyProc @fname AS VARCHAR(15), @lname AS VARCHAR(25)
AS
SELECT * FROM Employees
WHERE last_name = COALESCE(@lname, last_name)
AND first_name = COALESCE(@fname, first_name)
The performance purists will probably chime in that it is better to
explicitly trap for the NULL parameters:
WHERE (@lname IS NOT NULL OR last_name = @lname)
...etc.
This has the advantage of handling combinations of multiple columns in
the criteria.
Joe
[quoted text, click to view] rodchar wrote:
> hey all,
> i'm trying to create a stored proc that has the following flexibility:
>
> select * from employees
> where firstname = "%searchclause%"
>
> select * from employees
> where lastname = "%searchclause%"
>
> my first guess is to have a parameter indicating which field the user wants
> to search and then have an if statement separating the 2 sql statements.
> would this be the best way?
>
> thanks,
> rodchar