SQL 错误处理   try
             end try     
                catch
                end catch    
 @@error请问 在Sql 中怎样处理错误, 能 给出一段代码,谢谢

解决方案 »

  1.   

    USE AdventureWorks;
    GO-- Verify that stored procedure does not exist.
    IF OBJECT_ID (N'usp_RethrowError',N'P') IS NOT NULL
        DROP PROCEDURE usp_RethrowError;
    GO-- Create the stored procedure to generate an error using 
    -- RAISERROR. The original error information is used to
    -- construct the msg_str for RAISERROR.
    CREATE PROCEDURE usp_RethrowError AS
        -- Return if there is no error information to retrieve.
        IF ERROR_NUMBER() IS NULL
            RETURN;    DECLARE 
            @ErrorMessage    NVARCHAR(4000),
            @ErrorNumber     INT,
            @ErrorSeverity   INT,
            @ErrorState      INT,
            @ErrorLine       INT,
            @ErrorProcedure  NVARCHAR(200);    -- Assign variables to error-handling functions that 
        -- capture information for RAISERROR.
        SELECT 
            @ErrorNumber = ERROR_NUMBER(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE(),
            @ErrorLine = ERROR_LINE(),
            @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');    -- Building the message string that will contain original
        -- error information.
        SELECT @ErrorMessage = 
            N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 
                'Message: '+ ERROR_MESSAGE();    -- Raise an error: msg_str parameter of RAISERROR will contain
        -- the original error information.
        RAISERROR 
            (
            @ErrorMessage, 
            @ErrorSeverity, 
            1,               
            @ErrorNumber,    -- parameter: original error number.
            @ErrorSeverity,  -- parameter: original error severity.
            @ErrorState,     -- parameter: original error state.
            @ErrorProcedure, -- parameter: original error procedure name.
            @ErrorLine       -- parameter: original error line number.
            );
    GO
      

  2.   

    -- Verify that stored procedure does not exist.
    IF OBJECT_ID (N'usp_GenerateError',N'P') IS NOT NULL
        DROP PROCEDURE usp_GenerateError;
    GO-- Create stored procedure that generates a constraint violation
    -- error. The error is caught by the CATCH block where it is 
    -- raised again by executing usp_RethrowError.
    CREATE PROCEDURE usp_GenerateError 
    AS 
        BEGIN TRY
            -- A foreign key constrain exists on the table. This 
            -- statement will generate a constraint violation error.
            DELETE FROM Production.Product
                WHERE ProductID = 980;
        END TRY
        BEGIN CATCH
            -- Call the procedure to raise the original error.
            EXEC usp_RethrowError;
        END CATCH;
    GO-- In the following batch, an error occurs inside 
    -- usp_GenerateError that invokes the CATCH block in
    -- usp_GenerateError. RAISERROR inside this CATCH block
    -- generates an error that invokes the outer CATCH
    -- block in the calling batch.
    BEGIN TRY  -- outer TRY
        -- Call the procedure to generate an error.
        EXECUTE usp_GenerateError;
    END TRY
    BEGIN CATCH  -- outer CATCH
        SELECT
            ERROR_NUMBER() as ErrorNumber,
            ERROR_MESSAGE() as ErrorMessage;
    END CATCH;
    GO
      

  3.   


    begin try
    select 1/0
    end try
    begin catch
    print 'error'
    end catch
      

  4.   

    你好 szx1999 如果要用 begin tran   保证数据一致