I'm keeping in the database a log of all the sessions for my
application. I'm trying to write a stored procedure that returns all
the sessions that; the login contains a certain string, loggedin after
a certain datetime and loggedout before another datetime. Any
combination of these parameters can be used and, if none, returns all
the log.
Below is the code I came up with but I'm having a "Syntax error
converting datetime from character string" exception. When not using
DateTime parameters everything works fine. Can you tell me how can I
avoid this exception? Thanks in advance...
ALTER PROCEDURE dbo.RetrieveAllSessionHistoryItemsContaining
(
@Pattern Varchar(255),
@From DateTime,
@To DateTime
)
AS
DECLARE @Query VARCHAR(500)
SET @Query = 'SELECT * FROM SessionHistoryItems, Sessions WHERE
SessionHistoryItems.SessionId = Sessions.SessionId'
DECLARE @conditions nvarchar(257)
SET @conditions = '';
IF LEN(@Pattern) > 0 BEGIN
SET @conditions = @conditions + ' Sessions.Login LIKE ''%' + @Pattern
+ '%'''
END
IF @From IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedOutAt >=
' + @From
END
IF @To IS NOT NULL BEGIN
IF LEN(@conditions) > 0 BEGIN
SET @conditions = @conditions + ' AND '
END
SET @conditions = @conditions + ' SessionHistoryItems.LoggedInAt <= '
+ @To
END
IF LEN(@conditions) > 0 BEGIN
EXEC(@Query + ' AND ' + @conditions)
END
ELSE BEGIN
EXEC(@Query)
END
RETURN