SQL XP 11
SQL XP 11
Objectives
In this lesson, you will learn to:
Create a transaction
Commit a transaction
Rollback a transaction
Rollback part of a transaction
Declare cursors
Implementing Transactions and Cursors
Objectives (Contd.)
Open cursors
Fetch data from cursors
Close cursors
Implementing Transactions and Cursors
Task List
Identify how to prevent inconsistency in data
Execute the transaction
Verify that the data has been updated in both tables
Implementing Transactions and Cursors
Consistency
Isolation
Durability
Implementing Transactions and Cursors
Just a Minute
Identify the following properties of a single unit of work:
Any data modification made by concurrent transactions
must be isolated from the modifications made by other
concurrent transactions
All the data modifications are performed or none of them
are performed
Any change in data by a completed transaction remains
permanently in effect in the system
All data is in a consistent state after a transaction is
completed successfully
Implementing Transactions and Cursors
Task List
Identify how to revert the changes made
Execute the transaction
Verify whether the transaction was executed
Implementing Transactions and Cursors
UPDATE Position
SET iCurrentStrength=iCurrentStrength + 10
WHERE cPositionCode='0015'
Implementing Transactions and Cursors
Task List
Identify how to break the transaction into parts
Execute the transaction
Verify the execution of the transaction
Implementing Transactions and Cursors
UPDATE Requisition
SET siNoOfVacancy=siNoOfVacancy - 10
WHERE cRequisitionCode='000004'
UPDATE Position
SET iCurrentStrength=iCurrentStrength+10
WHERE cPositionCode='0015'
Implementing Transactions and Cursors
Cursors
A cursor is a database object that helps in accessing and
manipulating data in a given result set
Cursors enable the processing of rows in the result set in the
following ways:
Allow specific rows to be retrieved from the result set
Allow the current row in the result set to be modified
Help navigate from the current row in the result set to a
different row
Allow data modified by other users to be visible in the
result set
Implementing Transactions and Cursors
Structure of Cursors
The following tasks need to be performed while using a
cursor in SQL Server:
The cursor needs to be defined and its attributes need to
be set.
The cursor needs to be opened.
The required rows need to be fetched from the cursor.
The data in the current row of the cursor can be modified,
if required.
The cursor needs to be closed.
The cursor should be deallocated. This is a good practice
as resources used by the cursor are released.
Implementing Transactions and Cursors
Task List
Identify the steps required to create the report
Execute the statements required to create the report
Verify that the output is as per the required results
Implementing Transactions and Cursors
CLOSE cursor_name
Implementing Transactions and Cursors
Summary
In this lesson, you learned that:
A transaction is created using the BEGIN TRANSACTION
statement.
A transaction can be committed by issuing the COMMIT
TRANSACTION statement.
A transaction can be rolled back by issuing the ROLLBACK
TRANSACTION statement.
A transaction can be partially rolled back by saving part of it
by issuing the SAVE TRANSACTION statement.
Implementing Transactions and Cursors
Summary (Contd.)
A transaction can be created inside a stored procedure. The
BEGIN TRANSACTION, COMMIT TRANSACTION, and
ROLLBACK TRANSACTION form the SQL statements of the
procedure.
SQL Server uses the concept of locking to ensure
transactional integrity and database consistency.
Implementing Transactions and Cursors
Summary (Contd.)
All successful distributed transactions follow the two-phase
commit mode.
A cursor is a database object that helps in accessing and
manipulating row-by-row data in a given result set.
Cursors are implemented in the following sequence:
Declaring the cursor (DECLARE statement)
Opening the cursor (OPEN statement)
Fetching the row (FETCH statement)
Closing the cursor (CLOSE statement)
Releasing the cursor (DEALLOCATE statement)