Share via


@@ error (Transact-sql)

Son hata numarası ile döner Transact-SQLyürütülen deyimi.

Konu bağlantısı simgesi Transact-SQL Sözdizim Kuralları

Sözdizimi

@@ERROR

Dönüş Türleri

tamsayı

Açıklamalar

Eğer 0 döner önceki Transact-SQLdeyimi hata ile karşılaştı.

Önceki ifadenin bir sorunla karşılaştı hata numarasını döndürür. Hata sys.messages katalog görünümünde hatalardan biri ise, @@ error hata sys.messages.message_id sütun değeri içerir. @@ error hata numarası sys.messages içinde ilişkili metin görüntüleyebilirsiniz.

Çünkü @@ error temizlenmiş ve yürütülen her deyimi sıfırlamak, Doğrulanmakta olan deyimi hemen kontrol veya sonradan kontrol edilebilir yerel bir değişkene kaydedin.

try kullanın...Hataları işlemek için yapı yakalamak. ««««DENEYİN...YAKALAMAK da @@ error daha fazla hata bilgi döndüren destekleyen ek sistem işlevleri (ERROR_LINE, error_message, error_procedure, ERROR_SEVERITY ve error_state) oluşturun. DENEYİN...YAKALAMAK da hemen bir hata deyiminden hata numarasını dönen için sınırlı bir error_number işlevini destekler. Daha fazla bilgi için, bkz. DENEYİN...catch (Transact-sql).

Örnekler

A.Belirli bir hata bulmak için @@ error kullanma

Aşağıdaki örnek @@ERRORkontrol etmek için bir onay kısıtlaması ihlali (hata # 547) bir UPDATEdeyimi.

USE AdventureWorks2012;
GO
UPDATE HumanResources.EmployeePayHistory
    SET PayFrequency = 4
    WHERE BusinessEntityID = 1;
IF @@ERROR = 547
    PRINT N'A check constraint violation occurred.';
GO

USE AdventureWorks2012;
GO
UPDATE HumanResources.EmployeePayHistory
    SET PayFrequency = 4
    WHERE BusinessEntityID = 1;
IF @@ERROR = 547
    PRINT N'A check constraint violation occurred.';
GO

B.Koşullu bir yordam çıkmak için @@ error kullanma

Aşağıdaki örnekler kullanır IF...ELSEtest etmek için ifadeleri @@ERRORsonra bir INSERTdeyimi saklı yordamdaki. Değeri @@ERRORdeğişken çağıran program başarılı veya başarısız işlem gösteren gönderilen dönüş kodu belirler.

USE AdventureWorks2012;
GO
-- Drop the procedure if it already exists.
IF OBJECT_ID(N'HumanResources.usp_DeleteCandidate', N'P') IS NOT NULL
    DROP PROCEDURE HumanResources.usp_DeleteCandidate;
GO
-- Create the procedure.
CREATE PROCEDURE HumanResources.usp_DeleteCandidate 
    (
    @CandidateID INT
    )
AS
-- Execute the DELETE statement.
DELETE FROM HumanResources.JobCandidate
    WHERE JobCandidateID = @CandidateID;
-- Test the error value.
IF @@ERROR <> 0 
    BEGIN
        -- Return 99 to the calling program to indicate failure.
        PRINT N'An error occurred deleting the candidate information.';
        RETURN 99;
    END
ELSE
    BEGIN
        -- Return 0 to the calling program to indicate success.
        PRINT N'The job candidate has been deleted.';
        RETURN 0;
    END;
GO

USE AdventureWorks2012;
GO
-- Drop the procedure if it already exists.
IF OBJECT_ID(N'HumanResources.usp_DeleteCandidate', N'P') IS NOT NULL
    DROP PROCEDURE HumanResources.usp_DeleteCandidate;
GO
-- Create the procedure.
CREATE PROCEDURE HumanResources.usp_DeleteCandidate 
    (
    @CandidateID INT
    )
AS
-- Execute the DELETE statement.
DELETE FROM HumanResources.JobCandidate
    WHERE JobCandidateID = @CandidateID;
-- Test the error value.
IF @@ERROR <> 0 
    BEGIN
        -- Return 99 to the calling program to indicate failure.
        PRINT N'An error occurred deleting the candidate information.';
        RETURN 99;
    END
ELSE
    BEGIN
        -- Return 0 to the calling program to indicate success.
        PRINT N'The job candidate has been deleted.';
        RETURN 0;
    END;
GO

C.@@ error @@ rowcount ile kullanma

Aşağıdaki örnek @@ERRORile @@ROWCOUNTişlemini doğrulamak için bir UPDATEdeyimi. Değeri @@ERRORhata herhangi bir gösterge için denetlenir ve @@ROWCOUNTgüncelleştirme için tablodaki bir sütun başarıyla uygulandığından emin olmak için kullanılır.

USE AdventureWorks2012;
GO
IF OBJECT_ID(N'Purchasing.usp_ChangePurchaseOrderHeader',N'P')IS NOT NULL
    DROP PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader;
GO
CREATE PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader
    (
    @PurchaseOrderID INT
    ,@BusinessEntityID INT
   )
AS
-- Declare variables used in error checking.
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;

-- Execute the UPDATE statement.
UPDATE PurchaseOrderHeader 
    SET BusinessEntityID = @BusinessEntityID 
    WHERE PurchaseOrderID = @PurchaseOrderID;

-- Save the @@ERROR and @@ROWCOUNT values in local 
-- variables before they are cleared.
SELECT @ErrorVar = @@ERROR
    ,@RowCountVar = @@ROWCOUNT;

-- Check for errors. If an invalid @BusinessEntityID was specified,
-- the UPDATE statement returns a foreign key violation error #547.
IF @ErrorVar <> 0
    BEGIN
        IF @ErrorVar = 547
            BEGIN
                PRINT N'ERROR: Invalid ID specified for new employee.';
                 RETURN 1;
            END
        ELSE
            BEGIN
                PRINT N'ERROR: error '
                    + RTRIM(CAST(@ErrorVar AS NVARCHAR(10)))
                    + N' occurred.';
                RETURN 2;
            END
    END

-- Check the row count. @RowCountVar is set to 0 
-- if an invalid @PurchaseOrderID was specified.
IF @RowCountVar = 0
    BEGIN
        PRINT 'Warning: The BusinessEntityID specified is not valid';
        RETURN 1;
    END
ELSE
    BEGIN
        PRINT 'Purchase order updated with the new employee';
        RETURN 0;
    END;
GO

USE AdventureWorks2012;
GO
IF OBJECT_ID(N'Purchasing.usp_ChangePurchaseOrderHeader',N'P')IS NOT NULL
    DROP PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader;
GO
CREATE PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader
    (
    @PurchaseOrderID INT
    ,@BusinessEntityID INT
   )
AS
-- Declare variables used in error checking.
DECLARE @ErrorVar INT;
DECLARE @RowCountVar INT;

-- Execute the UPDATE statement.
UPDATE PurchaseOrderHeader 
    SET BusinessEntityID = @BusinessEntityID 
    WHERE PurchaseOrderID = @PurchaseOrderID;

-- Save the @@ERROR and @@ROWCOUNT values in local 
-- variables before they are cleared.
SELECT @ErrorVar = @@ERROR
    ,@RowCountVar = @@ROWCOUNT;

-- Check for errors. If an invalid @BusinessEntityID was specified,
-- the UPDATE statement returns a foreign key violation error #547.
IF @ErrorVar <> 0
    BEGIN
        IF @ErrorVar = 547
            BEGIN
                PRINT N'ERROR: Invalid ID specified for new employee.';
                 RETURN 1;
            END
        ELSE
            BEGIN
                PRINT N'ERROR: error '
                    + RTRIM(CAST(@ErrorVar AS NVARCHAR(10)))
                    + N' occurred.';
                RETURN 2;
            END
    END

-- Check the row count. @RowCountVar is set to 0 
-- if an invalid @PurchaseOrderID was specified.
IF @RowCountVar = 0
    BEGIN
        PRINT 'Warning: The BusinessEntityID specified is not valid';
        RETURN 1;
    END
ELSE
    BEGIN
        PRINT 'Purchase order updated with the new employee';
        RETURN 0;
    END;
GO

Ayrıca bkz.

Başvuru

DENEYİN...catch (Transact-sql)

ERROR_LINE (Transact-sql)

error_message (Transact-sql)

error_number (Transact-sql)

error_procedure (Transact-sql)

ERROR_SEVERITY (Transact-sql)

error_state (Transact-sql)

@@ rowcount (Transact-sql)

sys.messages (Transact-sql)