在 SQL Server 2008 R2 中引发错误

Throw error in SQL Server 2008 R2

我正在使用 SQL Server 2008 R2,我有一个存储过程,我试图在其中抛出错误

throw 50001, 'Couldnot process,Please verify Transaction ID and EmbossLine', 1

但是查询没有执行并在 50001 上抛出错误。它在 SQL Server 2012 上运行良好。我认为版本存在一些问题。有没有其他方法可以在 SQL Server 2008 R2 中引发错误?

这是我的存储过程:

Alter procedure [dbo].[spGetRedemption]
    @pan varchar(19),
    @transId bigint
AS 
Begin
    if EXISTS(select * from POS_Transactions where ID=@transId) 
    Begin
       select 
           PT.ID, PT.TransactionDate, M.MerchantName1, 
           PT.TerminalID, PT.BatchNumber, PT.SequenceNumber, PT.PAN,
           C.EmbossName, PT.TotalAmount, PT.CurrencyCode, 
           TT.TransactionType, PT.InvoiceNumber 
       from 
           POS_Transactions PT
       inner join 
           Terminal T on T.TerminalID = PT.TerminalID
       inner join 
           Merchant M on M.MerchantID = T.MerchantID
       inner join 
           Card C on C.EmbossLine = PT.PAN
       inner join 
           TransactionType TT on TT.TransactionTypeID = PT.TransactionTypeID
       where 
           PT.ID = @transId
           and PT.PAN = @pan
    END
    Else
    Begin
         throw 50001, 'Couldnot process,Please verify Transaction ID and EmbossLine', 1
    END
End

Throw 在 SQL Server 2008R2 中不可用;它首次在 SQL Server 2012 中引入。

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

另一种方法是使用 Raiserror(注意;中间只有 1 个 E;不是 RaiseError)。

从上面link来看,这些方法之间存在一些差异:

RAISERROR statement

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

The msg_str parameter can contain printf formatting styles.

The severity parameter specifies the severity of the exception.

THROW statement

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

The message parameter does not accept printf style formatting.

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