
Note: in the USA a checking account is like a current account in India

You can set the database connection to implicit transaction mode by using SET IMPLICIT TRANSACTIONS ON|OFF.
After implicit transaction mode has been set to ON for a connection, SQL Server automatically starts a transaction when it first executes any of the following statements: ALTER TABLE, CREATE, DELETE, DROP, FETCH, GRANT, INSERT, OPEN, REVOKE, SELECT, TRUNCATE TABLE, and UPDATE.
The transaction remains in effect until a COMMIT or ROLLBACK statement has been explicitly issued. This means that when, say, an UPDATE statement is issued on a specific record in a database, SQL Server will maintain a lock on the data scoped for data modification until either a COMMIT or ROLLBACK is issued. In case neither of these commands are issued, the transaction will be automatically rolled back when the user disconnects. This is why it is not a best practice to use implicit transaction mode on a highly concurrent database.
Get all required input from users before a transaction is started. If additional user input is required during a transaction then roll back the current transaction and restart the transaction after the user input is supplied. Even if users respond immediately, human reaction times are vastly slower than computer speeds. All resources held by the transaction are held for an extremely long time, that has the potential to cause blocking problems. If users do not respond then the transaction remains active, locking critical resources until they respond, that may not happen for several minutes or even hours.
Transactions should not be started until all preliminary data analysis has been completed.
After you know the modifications that need to be made, start a transaction, execute the modification statements, and then immediately commit or roll back. Do not open the transaction before it is required.
In a system with a low probability of concurrent updates, the overhead of dealing with an occasional “somebody else changed your data after you read it” error can be much lower than the overhead of always locking rows as they are read.
The smaller the amount of data that you access in the transaction, the fewer the number of rows that will be locked, reducing contention between transactions.
Listing 1-1. Create Parent-Child relationship

Figure 1-2. Executing the Create table statement (parent-child relationship)
Listing 1-2. Create Parent-Child relationship
Since a child can have only those records that map to the parent, hence we can only insert child records into PersonDetails for those PersonIDs that are already available in the Person table.

Similarly, the PersonDetails table is a foreign-key or child table, it has a PersonID column that is a foreign-key column and reference to Person.PersonID. It also has an Address column. The child or foreign-key table can only have those records that has a matching Primary-key column value available in the Parent or Primary-key table as shown in Figure 1-3 above, if a child record is inserted that doesn’t have a matching parent or primary ley value then it will result in an error and not insterted into the child table.
Listing 1-3. spTransTest

Figure 1-4. Executing the Stored Procedure in the same query window, enter the following SELECT statement:
Select the statement as shown in Figure 1-3 and then click the “Execute” button. You will see that the person named “Vamika” has been added to the table, as shown in the Results tab in Figure 1-3.

Figure 1-5. Row inserted in a transaction
You should get the same results as shown earlier in Figure 1-4 in the Messages tab.
as
These local variables will be used with the Stored Procedure, so you can capture and display the error numbers returned if any from the INSERT and DELETE statements.
Error handling is important at all times in SQL Server, and it’s never more than inside transactional code. When you execute a T-SQL statement, there’s always the possibility that it may not succeed. The T-SQL @@ERROR function returns the error number for the last T-SQL statement executed. If no error occurred then @@ERROR returns zero.
Tip: T-SQL (and standard SQL) supports various alternative forms for keywords and phrases. You’ve used just ROLLBACK and COMMIT here.
Now let’s look at what happens when you execute the Stored Procedure. You run it twice, first by adding person “Pearl” and next by adding person “Spark”, but you also enter the same nonexistent person “Agarw” to delete each time. If all statements in a transaction are supposed to succeed or fail as one unit then why does the INSERT succeed when the DELETE doesn’t delete anything?
The result should appear as in Figure 1-6.

The result should appear as in Figure 1-7.

The result should appear as in Figure 1-8.

Not a member yet? Register now
Are you a member? Login now