﻿CREATE PROCEDURE [dbo].[sp_getActivitiesLog]
            @dateFrom as datetime, 
            @dateTo as datetime ,
		    @activityDescription as nvarchar(50),
			@callingUser as nvarchar(50),
			@callingObject as nvarchar(50)
AS
-- 1 LogDate
if (@dateFrom IS NOT null and @dateTo IS NOT null) and (@activityDescription IS NULL and @callingUser IS NULL and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK)  WHERE LogDate between  @dateFrom  AND @dateTo ORDER BY LogDate DESC
-- 2 activityDescription
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NOT null and @callingUser IS NULL and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) WHERE activityDescription LIKE  '%' + @activityDescription + '%'
-- 3 callingUser
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NULL and @callingUser IS NOT null and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) WHERE  callingUser LIKE '%' + @callingUser + '%'
-- 4 callingObject
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NULL and @callingUser IS NULL and @callingObject IS NOT null)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) WHERE  callingObject LIKE '%' + @callingObject + '%'
--1 2 LogDate & activityDescription
if (@dateFrom IS NOT null and @dateTo IS NOT null) and (@activityDescription IS NOT null and @callingUser IS NULL and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) WHERE (LogDate between  @dateFrom  AND @dateTo) and activityDescription LIKE  '%' + @activityDescription + '%'
--1 3 LogDate & callingUser
if (@dateFrom IS NOT null and @dateTo IS NOT null) and (@activityDescription IS NULL and @callingUser IS NOT null and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE (LogDate between  @dateFrom  AND @dateTo)  and callingUser LIKE '%' + @callingUser + '%'
--1 4 LogDate & callingObject
if (@dateFrom IS NOT null and @dateTo IS NOT null and @activityDescription IS NULL and @callingUser IS NULL and @callingObject IS NOT null)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE (LogDate between  @dateFrom  AND @dateTo)  and callingObject LIKE '%' + @callingObject + '%'
--2 3 activityDescription & callingUser
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NOT null and @callingUser IS NOT null and @callingObject IS NULL) 
              SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE activityDescription LIKE  '%' + @activityDescription + '%'  and callingUser LIKE '%' + @callingUser + '%'
--2 3 4 activityDescription & callingUser & callingObject
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NOT null and @callingUser IS NOT null and @callingObject IS NOT null)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE activityDescription LIKE  '%' + @activityDescription + '%'  and callingUser LIKE '%' + @callingUser + '%' and callingObject LIKE '%' + @callingObject + '%'
--2 4 activityDescription & callingObject
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NOT null and @callingUser IS NULL and @callingObject IS NOT null) 
              SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE activityDescription LIKE  '%' + @activityDescription + '%'  and callingObject LIKE '%' + @callingObject + '%'
-- 3 4 callingUser & callingObject
if (@dateFrom IS NULL or @dateTo IS NULL) and (@activityDescription IS NULL and @callingUser IS NOT null and @callingObject IS NOT null)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE callingUser LIKE '%' + @callingUser + '%' and callingObject LIKE '%' + @callingObject + '%'
--1 2 3 LogDate & activityDescription & callingUser
if (@dateFrom IS NOT null and @dateTo IS NOT null) and (@activityDescription IS NOT null and @callingUser IS NOT null and @callingObject IS NULL)  
            SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities WITH (NOLOCK) WHERE (LogDate between  @dateFrom  AND @dateTo) and activityDescription LIKE  '%' + @activityDescription + '%'  and callingUser LIKE '%' + @callingUser + '%'
--1 2 3 4 LogDate & activityDescription & callingUser & callingObject
if (@dateFrom IS NOT null and @dateTo IS NOT null) and (@activityDescription IS NOT null and @callingUser IS NOT null and @callingObject IS NOT null)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) WHERE (LogDate between  @dateFrom  AND @dateTo) and activityDescription LIKE  '%' + @activityDescription + '%'  and callingUser LIKE '%' + @callingUser + '%' and callingObject LIKE '%' + @callingObject + '%'
--0 
if (@dateFrom IS NULL and @dateTo IS NULL and @activityDescription IS NULL and @callingUser IS NULL and @callingObject IS NULL)  
             SELECT  LogId, LogDate, activityDescription, CallingUser, CallingObject  FROM tActivities  WITH (NOLOCK) ORDER BY LogDate DESC