I want to build a single select stored procedure for SQL 2005 that is universal for any select query on that table.
**Columns**
LocationServiceID
LocationID
LocationServiceTypeID
ServiceName
ServiceCode
FlagActive
For this table I may need to select by LocationServiceID, or LocationID, or LocationServiceTypeID or ServiceName or a combination of the above.
I'd rather not have a separate stored procedure for each of them.
I assume the best way to do it would be to build the 'WHERE' statement on NOT NULL. Something like
SELECT * FROM LocationServiceType WHERE
IF @LocationID IS NOT NULL (LocationID = @LocationID)
IF @LocationServiceID IS NOT NULL (LocationServiceID = @LocationServiceID)
IF @LocationServiceTypeID IS NOT NULL (LocationServiceTypeID = @LocationServiceTypeID)
IF @ServiceName IS NOT NULL (ServiceName = @ServiceName)
IF @ServiceCode IS NOT NULL (ServiceCode = @ServiceCode)
IF @FlagActive IS NOT NULL (FlagActive = @FlagActive)
Does that make sense?