create procedure pro_test
begin
     do something  //ERROR在此發生
     if @@error<>0 goto 标签
     XXX....
     if @@error<>0 goto 标签
return
    标签:
         begin
            處理Error
         end
commit
end

解决方案 »

  1.   

    declare @ReturnMessage nvarchar(100)
    select @ReturnMessage = N'积分%d排名%d'           EXEC master.dbo.sp_addmessage 50001, 16, @ReturnMessage,'us_english'
                    
    select @ReturnMessage = FORMATMESSAGE(50001, 12,34)EXEC sp_dropmessage 50001,'us_english'
    select * from sysmessages where error=50001set @@ERROR
    select * from master.dbo.sysmessages where error =@@ERROR 
    if @@error <> 0 
      RAISERROR ('there have an error.', 16, 1)
      

  2.   

    是這樣處理的嗎?
    那請問,我在sysmessages中查到的資料狀況是:
    2548 10 0 DBCC: 索引 '%1!' 的壓縮階段已完成 %3!%%。 1028
    2548 10 0 DBCC: Compaction phase of index '%.*ls' is %d%% complete. 1033
    2549 10 0 DBCC: 索引 '%1!' 的重組階段已完成 %3!%%。 1028
    2549 10 0 DBCC: Defrag phase of index '%.*ls' is %d%% complete. 1033
    2557 14 0 使用者 '%1!' 沒有執行 '%4!' 物件的 DBCC %3! 的相關權限。 1028
    2557 14 0 User '%.*ls' does not have permission to run DBCC %ls for object '%.*ls'. 1033
    2559 16 1 不允許在同一個陳述式中同時使用選項 '%1!' 和 '%2!'。 1028
    2559 16 1 The '%ls' and '%ls' options are not allowed on the same statement. 1033
    -------------------------------------------------------------------------
    每一個Error有兩筆資料,那我該取得哪一筆才算是正確的?2. 對於你所提出的Error處理情況,我再請問:
    在SQLSERVER中,ERROR發生時,它還會繼續執行嗎?還會去做下面的判斷嗎?那不是同我上面的處理是一樣的?另外照這來做,則我在一個上百行的procedure裡,不是要做很多if判斷?
      

  3.   

    好了,在外面找到了答案。
    http://www.developersdex.com/sql/message.asp?r=2869867&p=581謝謝力兄!