[英]SQL Server How Can I get a error message with error code?
LogDate ProcessInfo Text
...
2019-03-20 09:45:25.480 Logon 오류: 18456, 심각도: 14, 상태: 5.
2019-03-20 09:45:25.480 Logon Login failed for user 'NE\NEO$'. 원인: 제공된 이름과 일치하는 로그인을 찾을 수 없습니다. [클라이언트: <local machine>]
2019-03-20 09:45:48.260 Logon 오류: 18456, 심각도: 14, 상태: 5.
2019-03-20 09:45:48.260 Logon Login failed for user 'NE\NEO$'. 원인: 제공된 이름과 일치하는 로그인을 찾을 수 없습니다. [클라이언트: <local machine>]
...
When I execute sp_readerrorlog, I got these error messages. 当我执行sp_readerrorlog时,出现了这些错误消息。
(The error messages is really part of it in total error logs (错误消息实际上是错误日志总数中的一部分
and other ProcessInfo Value of error exists.) 并且存在其他ProcessInfo错误值。)
I want to receive error messages related to error codes. 我想接收与错误代码相关的错误消息。 ( It means two lines consisting of sets. And The error message what I want is not dependent on login.) My question is, How can I get error message with error code from query.
(这意味着由两行组成的行。而且我想要的错误消息不取决于登录。)我的问题是,如何从查询中获取带有错误代码的错误消息。
if object_id('tempdb..#log') is not null drop table #log;
create table #log (id int identity primary key clustered,
LogDate datetime,
ProcessInfo varchar(15),
txt varchar(8000));
insert into #log (LogDate, ProcessInfo, txt) exec xp_readerrorlog 0,1;
with cte as
(
select *,
lead(txt) over (order by id) as txt1
from #log
)
select *
from cte
where txt like 'Error:%';
Here is the output example: 这是输出示例:
To do it this way you would need to execute the stored procedure twice, once for each term you want to find. 为此,您将需要执行两次存储过程,每个要查找的术语一次。
You can combine these into a single query though with something like the following 您可以将它们合并为一个查询,但可以使用以下内容
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT * FROM OPENROWSET('SQLNCLI',
'Server= (local)\<instance_name>;Trusted_Connection=yes;',
'exec sp_readerrorlog 0, 1, ''오류:'' WITH RESULT SETS
((
EventTime nvarchar(25),
Login nvarchar(50),
Message nvarchar(4000)
))')
UNION
SELECT * FROM OPENROWSET('SQLNCLI',
'Server=(local)\<instance_name>;Trusted_Connection=yes;',
'exec sp_readerrorlog 0, 1, ''Login failed for user'' WITH RESULT SETS
((
EventTime nvarchar(25),
Login nvarchar(50),
Message nvarchar(4000)
))')
You will need to replace <instance_name>
with your instance 您将需要用
<instance_name>
替换<instance_name>
Further details about this process can be found on this question. 有关此过程的更多详细信息,请参见此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.