Pregunta

I want to run a series of SQL INSERT statements. The problem is I want an all-or-nothing approach. Either they all execute or if one of them doesn't execute then no changes are made to the database.

The only solution i can think of is using a conditional loop but that would mean a lot of redundant code (determining changes made, dropping tables etc) Is there a simpler solution?

I have searched extensively for a solution, but didnt find any similar questions, so apologies if it has been asked before

¿Fue útil?

Solución

You need to use a Transaction, you can find an MSDN example here.

Otros consejos

Fortunately, a good database is one that has the four ACID properties - Atomic, Consistent, Integrated and Durable.

The first property- Atomic - refers to the behavior of transactions where either the entire transaction is commited at a time or no changes take place at all.

Read Korth's book "Database System Concepts" for further reference.

If you are using an outstanding database like Oracle, MS SQL Server, MySQL, DB2, etc., all you have to do is study a little bit about how they handle transactions and place your DML statements within these transactions.

Find out about Oracle's transactions support here.

P.S.- Looks like you're working in banking domain. These people are hell-bent on these things.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top