MCTS - Microsoft Certified Technology Specialist SQL Server Database Development Questions and Answers 1 — Questions and Answers
Question 1: A developer needs to ensure that a series of data modifications within a stored procedure are treated as a single, atomic unit. If an error occurs at any point, all prior modifications within the procedure should be undone. Which T-SQL construct is most appropriate for handling this requirement?
- A cursor to iterate through the statements.
- A TRY...CATCH block combined with a transaction. (Correct answer)
- A temporary table to log changes before applying them.
- An AFTER trigger on the target table.
Correct answer: A TRY...CATCH block combined with a transaction.
The combination of a TRY...CATCH block and a transaction is the standard and most robust method for ensuring atomicity in SQL Server. The data modification statements are placed within a BEGIN TRANSACTION...COMMIT TRANSACTION block, which is itself inside a TRY block. If an error occurs, control passes to the CATCH block, where a ROLLBACK TRANSACTION statement can be executed to undo all the changes made since the transaction began.
Question 2: You are designing a large table that will store order history. The most frequent query pattern will be to retrieve all orders for a specific date range. To optimize for this query pattern, which of the following columns would be the best candidate for the clustered index key?
- A UNIQUEIDENTIFIER column populated with NEWID().
- The OrderDate column, which is of the DATETIME data type. (Correct answer)
- A combination of CustomerID and ProductID.
- A NVARCHAR(255) column for order notes.
Correct answer: The OrderDate column, which is of the DATETIME data type.
A clustered index determines the physical order of data in a table. For range queries, such as retrieving orders within a specific date range, using the 'OrderDate' column as the clustered index key is highly efficient. This is because all the data for a given range will be stored physically close together, minimizing I/O operations. A UNIQUEIDENTIFIER created with NEWID() would lead to random inserts and heavy page splitting, while a composite key on CustomerID and ProductID is less optimal for the primary query pattern described.
Question 3: A developer needs to write a complex query that involves several levels of aggregation and joins. To improve readability and maintainability, they want to break the query into logical, named steps. The intermediate result set from one step will be used immediately in the next step within the same query. Which SQL Server feature should be used?
- Temporary Table
- Table Variable
- Common Table Expression (CTE) (Correct answer)
- View
Correct answer: Common Table Expression (CTE)
A Common Table Expression (CTE), defined using the WITH clause, is ideal for improving the readability and structure of complex queries. It allows you to define a temporary, named result set that exists only for the duration of a single statement. This is perfect for multi-step logical operations within one query without the overhead of creating a physical temporary table in tempdb.
Question 4: You have three AFTER INSERT triggers defined on a single table, `dbo.Orders`. There is a business requirement that `TriggerA` must execute before `TriggerB`, and `TriggerC` must execute after `TriggerB`. How can you enforce this specific execution order?
- Rename the triggers alphabetically (e.g., trg_1_A, trg_2_B, trg_3_C).
- Combine all trigger logic into a single trigger.
- Use the `sp_settriggerorder` system stored procedure. (Correct answer)
- Set the trigger recursion server configuration option.
Correct answer: Use the `sp_settriggerorder` system stored procedure.
SQL Server does not guarantee the execution order of multiple triggers on the same event by default. The `sp_settriggerorder` system stored procedure is specifically designed to control this behavior by allowing you to designate one trigger as the 'First' and another as the 'Last' to fire for a specific action (INSERT, UPDATE, or DELETE). By setting TriggerA as 'First' and TriggerC as 'Last', the desired order can be achieved.
Question 5: Which of the following statements about error handling with TRY...CATCH blocks in T-SQL is TRUE?
- They can catch compilation errors, such as syntax errors.
- A TRY block must be immediately followed by exactly one CATCH block within the same batch. (Correct answer)
- They can catch errors with a severity level of 21.
- The `ROLLBACK` command is automatically executed within the CATCH block.
Correct answer: A TRY block must be immediately followed by exactly one CATCH block within the same batch.
A TRY...CATCH construct requires that the CATCH block immediately follows the END TRY statement of its corresponding TRY block, with no other statements in between, and they must exist in the same batch of T-SQL code. They cannot catch compile-time syntax errors or severe errors (severity 20 or higher) that terminate the session. Furthermore, transactions must be explicitly rolled back within the CATCH block; it is not an automatic action.
Question 6: A developer is writing a stored procedure to insert a new employee record. The procedure must also add a corresponding record to the `EmployeeAudit` table, logging the user who performed the action. Which of the following is the most appropriate tool to ensure the audit record is always created when a new employee is successfully inserted, even if the insert happens outside of the stored procedure?
- A scheduled SQL Server Agent job.
- A Common Table Expression (CTE) within the insert procedure.
- An INSTEAD OF INSERT trigger on the `Employee` table.
- An AFTER INSERT trigger on the `Employee` table. (Correct answer)
Correct answer: An AFTER INSERT trigger on the `Employee` table.
An AFTER INSERT trigger is a special type of stored procedure that automatically executes after a data modification event (in this case, an INSERT) occurs on a table. By placing the logic to insert a record into the `EmployeeAudit` table within an AFTER INSERT trigger on the `Employee` table, you guarantee that the audit action will occur whenever an insert is successfully committed, regardless of the application or method used to perform the insert.
A developer needs to ensure that a series of data modifications within a stored procedure are treated as a single, atomic unit.
If an error occurs at any point, all prior modifications within the procedure should be undone.
Which T-SQL construct is most appropriate for handling this requirement?