###BeginCaseStudy###
Case Study: 2
Scenario 2
Application Information
You have two servers named SQL1 and SQL2 that have SQL Server 2012 installed.
You have an application that is used to schedule and manage conferences.
Users report that the application has many errors and is very slow.
You are updating the application to resolve the issues.
You plan to create a new database on SQL1 to support the application. A junior database
administrator has created all the scripts that will be used to create the database. The script that
you plan to use to create the tables for the new database is shown in Tables.sql. The script
that you plan to use to create the stored procedures for the new database is shown in
StoredProcedures.sql. The script that you plan to use to create the indexes for the new
database is shown in Indexes.sql. (Line numbers are included for reference only.)
A database named DB2 resides on SQL2. DB2 has a table named SpeakerAudit that will
audit changes to a table named Speakers.
A stored procedure named usp_UpdateSpeakersName will be executed only by other stored
procedures. The stored procedures executing usp_UpdateSpeakersName will always handle
transactions.
A stored procedure named usp_SelectSpeakersByName will be used to retrieve the names of
speakers. Usp_SelectSpeakersByName can read uncommitted data.
A stored procedure named usp_GetFutureSessions will be used to retrieve sessions that will
occur in the future.
Procedures.sql
Indexes.sql
Tables.sql
###EndCaseStudy###
You need to ensure that if any of the statements in usp_UpdateSpeakerName return an
error message, all of the changes executed by usp_UpdateSpeakerName are not committed
to the database.
What should you do in Procedures.sql? (Each correct answer presents part of the solution.
Choose all that apply.)
A.
Option A
B.
Option B
C.
Option C
D.
Option D
E.
Option E
C,D
procedure is always part of a transaction so we need to save the point to roll it back to the state it was before. If we create a new transaction, the named rollback will result in an error, as the rollback must always have the name of the outermost transaction or a savepoint
CD are correct answer, but not because stored proc imply transaction, but because a simple rollback will rollback everything, which is may not be the intent of the person who use the proc. rollback with a name solve this problem.
CREATE PROCEDURE usp_UpdateSpeakerName
@SpeakerID int,
@LastName nvarchar(100)
AS
SAVE TRANSACTION SpeakerUpdate
BEGIN TRY
UPDATE Speakers
SET LastName = @LastName
WHERE SpeakerID = @SpeakerID;
INSERT INTO SpeakerAudit(SpeakerID, LastName)
VALUES (@SpeakerID, @LastName);
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION SpeakerUpdate
END CATCH;
GO
ALTER PROCEDURE usp_UpdateSpeakerName
@SpeakerID int,
@LastName nvarchar(100)
AS
BEGIN TRANSACTION SpeakerUpdate
BEGIN TRY
UPDATE Speakers
SET LastName = @LastName
WHERE SpeakerID = @SpeakerID;
INSERT INTO SpeakerAudit(SpeakerID, LastName)
VALUES (@SpeakerID, @LastName);
COMMIT TRANSACTION SpeakerUpdate
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION SpeakerUpdate
END CATCH;
GO