Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Si applica a:
Databricks SQL
Avvia una nuova transazione interattiva che raggruppa più istruzioni SQL in una singola unità di lavoro di cui è possibile eseguire il commit o il rollback.
In alternativa alle transazioni interattive, è possibile definire transazioni non interattive usando la BEGIN ATOMIC ... END; sintassi . Consultare Istruzione composta ATOMIC.
Sintassi
BEGIN { TRANSACTION | WORK }
Parametri
Questa istruzione non ha parametri.
Note
- Se una transazione interattiva è già attiva, l'esecuzione di
BEGIN TRANSACTIONrestituisce un errore TRANSACTION_NOT_SUPPORTED.NESTED_TRANSACTION. Le transazioni interattive non supportano l'annidamento. -
BEGIN TRANSACTIONeBEGIN WORKsono alternative di sintassi equivalenti.
Examples
Gli esempi seguenti illustrano modelli di transazione interattivi comuni.
Transazione incrociata di base tra tabelle
Eseguire ogni istruzione separatamente.
BEGIN TRANSACTION;
-- Insert a new record
INSERT INTO my_table VALUES (1, 'test');
-- Update records in another table
UPDATE another_table
SET value = 'updated'
WHERE id = 5;
-- Commit all changes in both tables atomically
COMMIT TRANSACTION;
Lettura-proprie-scritture
Eseguire ogni istruzione separatamente.
BEGIN TRANSACTION;
-- Insert new data
INSERT INTO customers VALUES (101, 'New Customer');
-- Query can see the inserted data within the same transaction
SELECT * FROM customers WHERE id = 101;
-- Returns the newly inserted row even though it's not yet committed
-- Update the newly inserted data
UPDATE customers SET name = 'Updated Customer Name' WHERE id = 101;
-- Query sees the updated value
SELECT name FROM customers WHERE id = 101;
-- Returns 'Updated Customer Name'
-- Commit makes changes visible to other transactions
COMMIT TRANSACTION;
Transazione con rollback
Eseguire ogni istruzione separatamente.
BEGIN TRANSACTION;
-- Attempt an insert operation
INSERT INTO my_table VALUES (1, 'incorrect-value');
-- After discovering the mistake, rollback the transaction
-- (no changes are actually made to the tables)
ROLLBACK TRANSACTION;
Visibilità delle modifiche non confermate
In questo esempio le modifiche all'interno di una transazione non sono visibili ad altre sessioni finché non viene eseguito il commit della transazione. Eseguire ogni istruzione separatamente.
Sessione 1:
BEGIN TRANSACTION;
-- Insert new data
INSERT INTO products VALUES (999, 'New Product', 29.99);
-- At this point, Session 2 cannot see this data
-- You can see your own changes
SELECT * FROM products WHERE id = 999;
-- Returns the new product
Sessione 2 (simultanea):
-- This query does not see the uncommitted data from Session 1
SELECT * FROM products WHERE id = 999;
-- Returns no rows
Sessione 1 (continua):
-- Now commit the transaction
COMMIT TRANSACTION;
Sessione 2 (dopo il commit):
-- Now the query can see the committed data
SELECT * FROM products WHERE id = 999;
-- Returns the new product
Visibilità delle modifiche simultanee
In questo esempio, le modifiche simultanee apportate all'esterno della transazione non sono visibili alla transazione. Azure Databricks acquisisce uno snapshot coerente di ogni tabella al primo accesso e tutte le letture successive di tale tabella usano questo snapshot (lettura ripetibile). Eseguire ogni istruzione separatamente.
Sessione 1 (avviare una transazione e leggere la tabella):
BEGIN TRANSACTION;
-- Read the table; captures a snapshot of orders at this point
SELECT COUNT(*) FROM orders;
-- Returns: 1
Sessione 2 (la sessione simultanea aggiunge una riga):
INSERT INTO orders VALUES (2, 'Product B', 75.00);
COMMIT;
Sessione 1 (continuata, rileggere la tabella):
-- Still reads from the original snapshot; the new row is not visible
SELECT COUNT(*) FROM orders;
-- Returns: 1 (unchanged, even though Session 2 committed a new row)
COMMIT;
Ciò garantisce letture coerenti in tutta la transazione, indipendentemente dalle modifiche simultanee.
Articoli correlati
- Istruzione composta ATOMIC (transazioni non interattive): Esecuzione di più istruzioni SQL come una singola transazione atomica con procedura di commit e rollback automatici
- COMMIT: eseguire il commit di una transazione interattiva e rendere permanenti tutte le modifiche
- ROLLBACK: eseguire il rollback di una transazione interattiva ed eliminare tutte le modifiche
- Transazioni: panoramica del supporto delle transazioni, dei requisiti e delle limitazioni
- Modalità di transazione: modelli dettagliati ed esempi per transazioni non interattive e interattive
-
Istruzione composta BEGIN END:
BEGIN ... ENDblocchi senza laATOMICparola chiave