Hi, I am trying to build a dynamic SQL query that does customer search. It accepts a bunch of parameters and dynamically creates select statement based on whether eaach parameter is null.
So say I have this
Create Procedure SearchCustomer
(
@CustomerName varchar(50),
@CustomerAddress varchar(100),
@Phone varchar(50
)
AS
Here I need to evaluate which parameter has value and create a statement based on that. So if say they pass customername and phone my dynamic statement needs to be
@Statement =
Select * from Customer where CustomerName=@CustomerName and Phone = @Phone
And then of course I probably need to dom something like
Exec(@Statement)
So what are the best practices of doing these things, especially when it comes to building statements and placing all the "AND"s and properly enclosing in quotes, etc
Thanks for any help!
So say I have this
Create Procedure SearchCustomer
(
@CustomerName varchar(50),
@CustomerAddress varchar(100),
@Phone varchar(50
)
AS
Here I need to evaluate which parameter has value and create a statement based on that. So if say they pass customername and phone my dynamic statement needs to be
@Statement =
Select * from Customer where CustomerName=@CustomerName and Phone = @Phone
And then of course I probably need to dom something like
Exec(@Statement)
So what are the best practices of doing these things, especially when it comes to building statements and placing all the "AND"s and properly enclosing in quotes, etc
Thanks for any help!