簡體   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>]
...

當我執行sp_readerrorlog時,出現了這些錯誤消息。
(錯誤消息實際上是錯誤日志總數中的一部分
並且存在其他ProcessInfo錯誤值。)

我想接收與錯誤代碼相關的錯誤消息。 (這意味着由兩行組成的行。而且我想要的錯誤消息不取決於登錄。)我的問題是,如何從查詢中獲取帶有錯誤代碼的錯誤消息。

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:%';

這是輸出示例:

在此處輸入圖片說明

為此,您將需要執行兩次存儲過程,每個要查找的術語一次。

您可以將它們合並為一個查詢,但可以使用以下內容

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)
  ))')

您將需要用<instance_name>替換<instance_name>

有關此過程的更多詳細信息,請參見此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM