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