I use SQL Server 2008 Express with Advanced Services. I have a view:
IF EXISTS (select * from sys.views where object_id = object_id(N'[dbo].[vw_PersonDetails]'))
DROP VIEW vw_PersonDetails
GO
CREATE VIEW vw_PersonDetails
AS
SELECT
p.PersonID, p.Title,
p.FirstName, p.LastName,
a.AddressLine1, a.AddressLine2, a.AddressLine3, a.AddressLine4,
a.Country, a.PostalCode,
a.PhoneNumber, a.Email, p.EntryDate
FROM
[dbo].[Persons] p
INNER JOIN
[dbo].[Address] a ON p.PersonID = a.PersonID
GO
Now I have to search this view with each of the columns as options.
Ex:
IF (@firstName != NULL OR @firstName != '') AND
(@lastName != NULL OR @lastName != '') AND
(@addressLine1 != NULL OR @addressLine1 != '') AND
(@postalCode != NULL OR @postalCode != '') AND
(@country != NULL OR @country != '') AND
(@phoneNumber != NULL OR @phoneNumber != '') AND
(@email != NULL OR @email != '') AND
(@entryDate != NULL)
BEGIN
SELECT *
FROM dbo.vw_PersonDetails
WHERE
(FirstName LIKE [dbo].[GetSearchString](@firstName) OR
LastName LIKE [dbo].[GetSearchString](@lastName) OR
AddressLine1 LIKE [dbo].[GetSearchString](@addressLine1) OR
Country LIKE [dbo].[GetSearchString](@country) OR
PostalCode LIKE [dbo].[GetSearchString](@postalCode) OR
PhoneNumber LIKE [dbo].[GetSearchString](@phoneNumber) OR
Email LIKE [dbo].[GetSearchString](@email) OR
EntryDate = @entryDate
)
END
Now is there any other options except writing a never ending IF-ELSE-IF trail in a stored procedure or building query dynamically. Please help.
Another question is that what will be better: Writing such a stored procedure or making a dynamic query from code.
Thanks in advance.