Querying Microsoft SQL Server 2012/2014 — Question 20

You develop a Microsoft SQL Server database that has two tables named SavingAccounts and LoanAccounts. Both tables have a column named AccountNumber of the nvarchar data type.
You use a third table named Transactions that has columns named TransactionId AccountNumber, Amount, and TransactionDate.
You need to ensure that when multiple records are inserted in the Transactions table, only the records that have a valid AccountNumber in the SavingAccounts or
LoanAccounts are inserted.
Which Transact-SQL statement should you use?

Answer options

Correct answer: A

Explanation

The correct answer is A because it uses an INSTEAD OF trigger to filter the inserted records based on valid AccountNumbers in the SavingAccounts and LoanAccounts tables before inserting them into the Transactions table. Option B incorrectly uses a FOR INSERT trigger, which would not allow for the desired filtering logic. Options C and D utilize an EXCEPT clause that would roll back the transaction instead of inserting valid records, making them ineffective for the requirement.