Annidare i costrutti TRY...CATCH
Vuoi registrare un nuovo acquirente nella tabella buyers. Il nuovo acquirente è Peter Thomson. La sua e-mail è [email protected] e il suo numero di telefono è 555000100.
Nel tuo database c’è anche una tabella chiamata errors, in cui viene memorizzato ogni errore.
Prepara uno script che gestisca i possibili errori nell’inserimento dei dati di questa persona. Inoltre, inserisce tali errori nella tabella errors.
Come prepari lo script?
Questo esercizio fa parte del corso
Transazioni e gestione degli errori in SQL Server
Istruzioni dell'esercizio
- Circonda l’istruzione
INSERT INTO buyerscon un bloccoTRY. - Circonda la gestione degli errori con un blocco
CATCH. - Circonda l’istruzione
INSERT INTO errorscon un altro bloccoTRY. - Circonda la gestione degli errori annidata con un altro blocco
CATCH.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
-- Set up the first TRY block
___ ___
INSERT INTO buyers (first_name, last_name, email, phone)
VALUES ('Peter', 'Thompson', '[email protected]', '555000100');
___ ___
-- Set up the first CATCH block
___ ___
SELECT 'An error occurred inserting the buyer! You are in the first CATCH block';
-- Set up the nested TRY block
___ ___
INSERT INTO errors
VALUES ('Error inserting a buyer');
SELECT 'Error inserted correctly!';
___ ___
-- Set up the nested CATCH block
___ ___
SELECT 'An error occurred inserting the error! You are in the nested CATCH block';
___ ___
___ ___