If it was a big query would it be faster on the second stored procedure ?
CREATE PROCEDURE Customers_GetCustomer
@CustId CHAR(5),
@type int,
@search nvarchar
AS
BEGIN
DECLARE @SQL NVARCHAR(2000)
SET @SQL = 'SELECT ContactName FROM Customers WHERE CustomerId = '+CONVERT(nvarchar(20), @CustId)
if(@type = 1)
BEGIN
@SQL += 'AND NAME='+@search
END
if(@type = 2)
BEGIN
@SQL += 'AND ADDRESS='+@search
END
EXEC sp_executesql @SQL
END
Versus:
CREATE PROCEDURE Customers_GetCustomer
@CustId CHAR(5),
@type int,
@search nvarchar
AS
BEGIN
SELECT ContactName FROM Customers
WHERE CustomerId = @CustId
AND (NAME = @search OR @type <> 1)
AND (ADDRESS = @search OR @type <> 2)
END
What is the better choice between first and second?