방법: Service Broker 오류 메시지에서 정보 검색(Transact SQL)

https://schemas.microsoft.com/SQL/ServiceBroker/Error 유형의 메시지는 Service Broker 오류 메시지입니다. 이 유형의 메시지는 오류를 나타내는 숫자 코드와 오류 설명이 포함된 XML 문서입니다.

Service Broker 오류 메시지에서 정보를 검색하려면

  1. int 유형의 변수를 선언하여 오류 코드를 포함합니다.

  2. nvarchar(3000) 유형의 변수를 선언하여 오류 설명을 포함합니다.

  3. xml 유형의 변수를 선언하여 메시지 본문의 XML 표현을 포함합니다.

  4. 메시지 본문을 **varbinary(max)**에서 xml로 형변환하고 xml 유형의 변수에 결과를 할당합니다.

  5. xml 데이터 유형의 value 함수를 사용하여 오류 코드를 검색합니다.

  6. xml 데이터 유형의 value 함수를 사용하여 오류 설명을 검색합니다.

  7. 응용 프로그램에 알맞게 오류를 처리합니다. Service Broker에서는 오류 코드가 음수인 오류를 생성하고 서비스 프로그램에서는 오류 코드가 양수인 오류를 생성합니다.

-- The variables to hold the error code and the description are
-- provided by the caller.

CREATE PROCEDURE [ExtractBrokerError]
  ( @message_body VARBINARY(MAX),
    @code int OUTPUT,
    @description NVARCHAR(3000) OUTPUT )
AS
BEGIN

-- Declare a variable to hold an XML version of the message body.

DECLARE @xmlMessage XML;

-- CAST the provided message body to XML.

SET @xmlMessage = CAST(@message_body AS XML);
SET @code = @@ERROR

IF @@ERROR<>0
  RETURN @code

-- Retrieve the error code from the Code element.

SET @code = (
      SELECT @xmlMessage.value(
        N'declare namespace
           brokerns="https://schemas.microsoft.com/SQL/ServiceBroker/Error";
               (/brokerns:Error/brokerns:Code)[1]', 
        'int')
        );

-- Retrieve the description of the error from the Description element.

SET @description = (
      SELECT @xmlMessage.value(
        'declare namespace
           brokerns="https://schemas.microsoft.com/SQL/ServiceBroker/Error";
           (/brokerns:Error/brokerns:Description)[1]', 
        'nvarchar(3000)')
        );


RETURN 0;

END
GO

참고 항목

개념

Broker 메시지

관련 자료

XQuery 기초

도움말 및 정보

SQL Server 2005 지원 받기