I have the following stored procedure which takes a user ID, a starting date, an end date, and a list of codes in a comma-delimited list, and it returns all activity records between those two dates which match one of the codes in the list.
ALTER PROCEDURE [dbo].[ActivitiesSummary]
@UserID varchar(30),
@StartDate datetime,
@EndDate datetime,
@Codes varchar(100)
AS
BEGIN
SET NOCOUNT ON;
SELECT act.SectionID, act.UnitID, act.ActivityCode
FROM dbo.Activities act INNER JOIN ConvertCodeListToTbl(@Codes) i ON act.ActivityCode = i.code
WHERE act.ActivityDate>=@Startdate AND act.ActivityDate<@EndDate
GROUP BY act.SectionID, act.UnitID, act.ActivityCode
ORDER BY act.SectionID, act.UnitID, act.ActivityCode
END
ConvertCodeListToTbl(@Codes) is a function that takes a comma-delimited list of codes (e.g., 'A0001, B0001, C0001') and returns a table with one code per row:
A0001
B0001
C0001
This method works really well except when no codes have been selected. When that occurs, I receive no records back because @Codes='' and the last INNER JOIN returns no records.
What I want to happen: if @Codes='', ignore the last INNER JOIN, or otherwise find a way to return all records regardless of code.
What are my options?