简体   繁体   English

SQL Server如何获取带有错误代码的错误消息?

[英]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.

相关问题 为什么我无法连接到我的sql数据库? 我收到错误消息:“无法通过套接字连接到本地MySQL服务器” - Why can't I connect to my sql database? I get the error message: “Can't connect to local MySQL server through socket” 我能以编程方式获取SQL错误详细信息吗? (在.Net和SQL Server中) - Can I get SQL error details programmatically? (in .Net and SQL Server) 如何更改默认错误消息代码 - How can I change the default error message code 如何抑制SQL Server备份错误消息 - How to suppress SQL Server backup error message 如何在使用 ExecuteNonQuery() 时收到错误消息? - How can I get an error message that happens when using ExecuteNonQuery()? 为什么在SQL Server中出现此错误? 如何解决? - Why do I get this error in SQL Server? How to fix it? 我在 Microsoft SQL Server 中收到此错误消息有什么问题? - What is wrong here for me to get this error message in Microsoft SQL Server? 如何使用 SQL 服务器查询修复此错误 - How can I fix this error with SQL server query 如何在 SQL Server 2005 中捕获截断错误? - How can I catch a truncation error in SQL Server 2005? 如何修复 SQL 中的 ORA-02270 错误代码? - How can I fix ORA-02270 error code in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM