存储过程中的多个错误处理

时间:2013-04-22 20:53:53

标签: sql sql-server tsql

我想知道是否有人能指出我正确的方向。这是我第一次尝试SQL中的任何类型的错误处理,我担心我完全错过了某些内容或者误解了错误处理的概念。

我有一个过程,基本上接受3个值,然后尝试查看其中一个值是否存在。如果是,则执行更新语句。

基本上,我想做以下事情: - 如果没有遇到错误,返回'1' - 如果Select语句无法找到记录,则返回错误 - 如果Update语句无法更新任何记录,则返回错误

以下是我的程序。目前,每当我运行它时,它返回一个'1',表示没有错误,这不是我预期它的工作方式,因为我传递了伪造的值并试图打破它。有人可以帮助指出我做错了什么或者这是否可能?

alter PROCEDURE [dbo].prc_update_SPRO_refill_status                             
@result_code char(2),                             
@result_string char(10),                                                      
@rx_id char(20)        


AS                            
BEGIN                            


SET NOCOUNT ON; 
BEGIN TRANSACTION                                
BEGIN TRY   

DECLARE @prescription_orders_id varchar(20)
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;

IF @result_code = 0
BEGIN
SELECT @prescription_orders_id = prescription_orders_id
FROM prescription_orders
WHERE rx_external_id = @rx_id
       -- Return error if record not found

UPDATE prescription_orders_fills
SET fill_status_code = 'R'
WHERE prescription_orders_id = @prescription_orders_id
    AND fill_status_code = 'P'
    AND fill_number > 1 
      -- return error if record not found
      -- return error if more then 1 record update


END                                               
END TRY
BEGIN CATCH
SELECT
    ERROR_NUMBER() AS ErrorNumber
    ,ERROR_SEVERITY() AS ErrorSeverity
    ,ERROR_STATE() AS ErrorState
    ,ERROR_PROCEDURE() AS ErrorProcedure
    ,ERROR_LINE() AS ErrorLine
    ,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
    ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
BEGIN
COMMIT TRANSACTION
 SELECT 1 as ERROR_NUMBER   
END                                                       
END 

2 个答案:

答案 0 :(得分:2)

我认为这里的问题很简单:当没有引发异常时,你期望TRY失败并使你进入CATCH块。想一想:以下查询是否因为没有更新行而引发错误?

CREATE TABLE #foo(a INT);

UPDATE #foo SET a = 1 WHERE a = 2;

此处没有错误消息,因此没有理由触发CATCH。如果要测试错误处理,请在其中放置真正的错误条件。例如:

BEGIN TRY
  IF @force_error = 1
  BEGIN
    SELECT 1/0;
  END
END TRY
BEGIN CATCH
  -- now you should get here...
END CATCH

正如我在评论中提到的那样 - 使用RETURN将错误号码/状态代码发送回呼叫者。没有理由调用记录集返回单个标量值所需的所有脚手架。

答案 1 :(得分:0)

我的2美分

alter PROCEDURE [dbo].prc_update_SPRO_refill_status                             
@result_code char(2),                             
@result_string char(10),                                                      
@rx_id char(20)        


AS                            
BEGIN                            


SET NOCOUNT ON;
  

SET XACT_ABORT ON; // 1,在尝试

之前实现这一点很好
BEGIN TRY   
BEGIN TRANSACTION  ; **// 2 start transaction after try**

DECLARE @prescription_orders_id varchar(20)
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;

IF @result_code = 0
    BEGIN
        ...
        ...
        ...
        ...  
    END                          

END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;

   IF @@TRANCOUNT > 0
    BEGIN
        COMMIT TRANSACTION;
        SELECT 1 as ERROR_NUMBER  ;
    END 

END