Denali - THROW

SQL Server 2011. 9. 3. 17:24

이전 버전에서 오류 메시지를 발생 시키기 위해서는 다음과 같이 오류코드를 등록한 다음 RAISERROR 명령을 이용할 있었습니다. 하지만 RAISERROR 명령의 경우 오류 메시지를 정의해 주어야만 사용할 있었기 때문에 사용하는데 불편함이 있었습니다.

EXEC sys.sp_addmessage

     @msgnum   = 60000

,    @severity = 16

,    @msgtext  = N'This is my error %s.';

 

RAISERROR(60000, 16, 1, N'manha');


SQL Denali
에서는 다음과 같이 2가지 방식을 통해 THROW명령을 사용할 있으며, 파라메터가 없는 경우 오류 메시지 정의 없이 사용할 있습니다.

-- 파라메터가 없는 경우

THROW 60000, N'my error message', 1;

-- 파라메터가 있는 경우

-- THROW 60000, N'my error message', 1, N'manha' 같은 형태로 사용할 있었으면 훨씬 훌륭했을것 같다는 생각입니다.

DECLARE @msg NVARCHAR(2048) = FORMATMESSAGE(60000, N'manha');

THROW 60000, @msg, 1;

 

== RAISERROR THROW 차이

RAISERROR statement

THROW statement

If a msg_id is passed to RAISERROR, the ID must be defined in sys.messages.

The error_number parameter does not have to be defined in sys.messages.

The msg_str parameter can contain printf formatting styles.

The message parameter does not accept printf style formatting.

The severity parameter specifies the severity of the exception.

There is no severity parameter. The exception severity is always set to 16.


[
참고]

SQL Server v.Next (Denali) : Exploring THROW

http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/22/sql-server-v-next-denali-using-throw-instead-of-raiserror.aspx

 

THROW (Transact-SQL)

http://msdn.microsoft.com/en-us/library/ee677615(v=sql.110).aspx


하만철 / Ha ManCheol

AND