SELECT * 
  FROM sys.messages
Hata mesajı eklemek:
sp_addmessage @msgnum=50001, @severity=11, @msgtext='Üye zaten sisteme kayıtlı', @with_log='true'
with_log = true: event log'a eklesin.
BEGIN TRY
    RAISERROR ('Errors found, please fix these errors and retry', 1, 2) WITH SETERROR
    // Complete normal process if no errors encountered above
    PRINT 'IMPORT SUCCEEDED'
END TRY
BEGIN CATCH
    PRINT 'IMPORT ABORTED. ERRORS ENCOUNTERED'
END CATCH
USE AdventureWorks;
GO
-- Declare and set variable
-- to track number of retries
-- to try before exiting.
DECLARE @retry INT;
SET @retry = 5;
--Keep trying to update 
-- table if this task is 
-- selected as the deadlock 
-- victim.
WHILE (@retry > 0)
BEGIN
    BEGIN TRY
       BEGIN TRANSACTION;
    
        UPDATE my_sales
        SET sales = sales + 1
        WHERE itemid = 2;
        WAITFOR DELAY '00:00:07';
    
        UPDATE my_sales
        SET sales = sales + 1
        WHERE itemid = 1;
        SET @retry = 0;
        COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH 
        -- Check error number.
        -- If deadlock victim error,
        -- then reduce retry count
        -- for next update retry. 
        -- If some other error
        -- occurred, then exit
        -- retry WHILE loop.
        IF (ERROR_NUMBER() = 1205)
            SET @retry = @retry - 1;
        ELSE
            SET @retry = -1;
        -- Print error information.
        EXECUTE usp_MyErrorLog;
  
        IF XACT_STATE() <> 0
            ROLLBACK TRANSACTION;
    END CATCH;
END; -- End WHILE loop.
GO
Hiç yorum yok:
Yorum Gönder