Fornire informazioni sugli errori

Transazioni e gestione degli errori in SQL Server

Miriam Antona

Software Engineer

Errore generato - ripasso

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).
Transazioni e gestione degli errori in SQL Server

Errore generato - ripasso

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 |
Transazioni e gestione degli errori in SQL Server

Funzioni di errore

ERROR_NUMBER() restituisce il numero dell'errore.

ERROR_SEVERITY() restituisce la gravità dell'errore (11-19).

ERROR_STATE() restituisce lo stato dell'errore.

ERROR_LINE() restituisce il numero di riga dell'errore.

ERROR_PROCEDURE() restituisce il nome della stored procedure/trigger. NULL se non c'è stored procedure/trigger.

ERROR_MESSAGE() restituisce il testo del messaggio di errore.

Transazioni e gestione degli errori in SQL Server

Funzioni di errore - esempi

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'... |
Transazioni e gestione degli errori in SQL Server

Funzioni di errore - esempi

Ultimo 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'... |

Informazioni sull'errore originale

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).
Transazioni e gestione degli errori in SQL Server

Funzioni di errore - esempi

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          |
Transazioni e gestione degli errori in SQL Server

Funzioni di errore in TRY...CATCH annidati

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. |
Transazioni e gestione degli errori in SQL Server

Passons à la pratique !

Transazioni e gestione degli errori in SQL Server

Preparing Video For Download...