Вопрос

I am trying to extract the login success and failures out of our SQL log file. This is the statement that I was able to get to work, and now I am getting an error when running it. Any help would be greatly appreciated. I know it is in the last statement because the rest works withouth error when running independently. Nothing has changed in the system so I don't understand why it would start giving the error?

CREATE TABLE [dbo].[#TmpErrorLog]
([LogDate] DATETIME NULL,
[ProcessInfo] VARCHAR(20) NULL,
[Text] VARCHAR(MAX) NULL);

CREATE TABLE [dbo].[#TmpErrorLog2]
([LogDate] DATETIME NULL,
[ProcessInfo] VARCHAR(20) NULL,
[Text] VARCHAR(MAX) NULL,
[LoginAttempt] VARCHAR(20) NULL);


INSERT INTO #TmpErrorLog ([LogDate], [ProcessInfo], [Text])   
EXEC [master].[dbo].[xp_readerrorlog] 0 ;

INSERT INTO #TmpErrorLog2 ([LogDate], [ProcessInfo], [Text], [LoginAttempt])
Select LogDate, ProcessInfo, Replace(Text,'''','"') as Text, SUBSTRING(Text,0,16)      as     LoginAttempt
From #TmpErrorLog
Where LogDate > GETDATE() - 1 and Text like '%\%'

INSERT INTO LogData ([LogDate], [LoginAttempt], [LoginUser])
Select 
LogDate, 
Case LoginAttempt When 'Login succeeded' Then 'Successfull' Else 'Failed' End as      LoginAttempt,
SUBSTRING(SUBSTRING(Text, CHARINDEX('"', Text,1), CHARINDEX('"', Text, CHARINDEX('"',     Text, 0)) - 4),2,50) as LoginUser
From #TmpErrorLog2
Where LogDate Not In(Select LogDate From LogData)

Drop Table #TmpErrorLog
Drop Table #TmpErrorLog2
Это было полезно?

Решение

IIRC, CHARINDEX can return -1, which would not be a valid parameter to SUBSTRING. I'm guessing you are getting some non-hits on your CHARINDEXes.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top