Informatie geven over fouten

Transacties en foutafhandeling in SQL Server

Miriam Antona

Software Engineer

Een fout krijgen - herhaling

INSERT INTO products (product_name, stock, price)
    VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'unique_name'. 
Cannot insert duplicate key in object 'dbo.products'. 
The duplicate key value is (Trek Powerfly 5 - 2018).
Transacties en foutafhandeling in SQL Server

Een fout krijgen - herhaling

BEGIN TRY
    INSERT INTO products (product_name, stock, price)
        VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
    SELECT 'Product inserted correctly!' AS message;
END TRY
BEGIN CATCH
    SELECT 'An error occurred! You are in the CATCH block' AS message;  
END CATCH
| message                                       |
|-----------------------------------------------|
| An error occurred! You are in the CATCH block |
Transacties en foutafhandeling in SQL Server

Foutfuncties

ERROR_NUMBER() geeft het foutnummer.

ERROR_SEVERITY() geeft de fouternst (11-19).

ERROR_STATE() geeft de foutstatus.

ERROR_LINE() geeft het regelnr. van de fout.

ERROR_PROCEDURE() geeft de naam van de stored procedure/trigger. NULL als er geen stored procedure/trigger is.

ERROR_MESSAGE() geeft de fouttekst.

Transacties en foutafhandeling in SQL Server

Voorbeelden van foutfuncties

BEGIN TRY
    INSERT INTO products (product_name, stock, price)
        VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
END TRY
BEGIN CATCH
    SELECT  ERROR_NUMBER() AS Error_number,  
            ERROR_SEVERITY() AS Error_severity,  
            ERROR_STATE() AS Error_state,  
            ERROR_PROCEDURE() AS Error_procedure,  
            ERROR_LINE() AS Error_line,  
            ERROR_MESSAGE() AS Error_message;             
END CATCH
| Error_number| Error_severity| Error_state| Error_procedure| Error_line| Error_message                                       |
|-------------|---------------|------------|----------------|-----------|-----------------------------------------------------|
| 2627        | 14            | 1          | NULL           | 2         | Violation of UNIQUE KEY constraint 'unique_name'... |
Transacties en foutafhandeling in SQL Server

Voorbeelden van foutfuncties

Laatste output

| Error_number| Error_severity| Error_state| Error_procedure| Error_line| Error_message                                       |
|-------------|---------------|------------|----------------|-----------|-----------------------------------------------------|
| 2627        | 14            | 1          | NULL           | 2         | Violation of UNIQUE KEY constraint 'unique_name'... |

Oorspronkelijke foutinformatie

Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'unique_name'. Cannot insert duplicate key in object 'dbo.products'. The duplicate key 
value is (Trek Powerfly 5 - 2018).
Transacties en foutafhandeling in SQL Server

Voorbeelden van foutfuncties

SELECT  ERROR_NUMBER() AS Error_number,  
        ERROR_SEVERITY() AS Error_severity,  
        ERROR_STATE() AS Error_state,  
        ERROR_PROCEDURE() AS Error_procedure,  
        ERROR_LINE() AS Error_line,  
        ERROR_MESSAGE() AS Error_message;
| Error_number | Error_severity | Error_state | Error_procedure | Error_line | Error_message |
|--------------|----------------|-------------|-----------------|------------|---------------|
| NULL         | NULL           | NULL        | NULL            | NULL       | NULL          |
Transacties en foutafhandeling in SQL Server

Foutfuncties in geneste TRY...CATCH-constructies

BEGIN TRY
    INSERT INTO products (product_name, stock, price)
        VALUES ('Trek Powerfly 5 - 2018', 10, 3499.99);
END TRY
BEGIN CATCH
    BEGIN TRY
        INSERT INTO myErrors
            VALUES ('ERROR!')
    END TRY    
    BEGIN CATCH
        SELECT  'Outer CATCH block' AS 'Error_from',
            ERROR_NUMBER() AS Error_number,       
            ERROR_MESSAGE() AS Error_message; 
    END CATCH    
END CATCH
| Error_from        | Error_number | Error_message                             |
|-------------------|--------------|-------------------------------------------|
| Outer CATCH block | 8152         | String or binary data would be truncated. |
Transacties en foutafhandeling in SQL Server

Laten we oefenen!

Transacties en foutafhandeling in SQL Server

Preparing Video For Download...