我正在尝试跟踪 SQL 登录成功和失败,并将它们存储在 SQL 表中以进行查询。我写了一个有效的查询,但我的问题是,当登录名包含域时,它可以正常工作,但如果它不包含域,它会添加额外的文本。我希望有人可以帮助我查看我的代码并查看如何删除登录名后的额外内容。
它返回的示例:Domain\BBruning
域\SQLServ
域\BSmith
域\JHarris'。
scgWeb'。原因:F
cpadmin'。原因:
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,
[ServerName] 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, Text, SUBSTRING(Text,0,16) as LoginAttempt
From #TmpErrorLog
Where LogDate > GETDATE() - 120 and
[ProcessInfo] = 'Logon' and
Text like '%Login%' and text not like '%untrusted%'
INSERT INTO [MyTABLE] ([LogDate], [LoginAttempt], [LoginUser], [ServerName], [Log_date_key])
Select
LogDate,
Case LoginAttempt When 'Login succeeded' Then 'Successful' ELSE 'Failed' End as LoginAttempt,
SUBSTRING(SUBSTRING(Text, CHARINDEX('''', Text,1), CHARINDEX('''', Text, 0) - 4),2,50) as LoginUser, @@SERVERNAME, (Convert(INT, LogDate, 112))
From #TmpErrorLog2
Where LogDate Not In(Select LogDate From [MyTable])
Drop Table #TmpErrorLog
Drop Table #TmpErrorLog2