Querying Microsoft SQL Server 2012/2014 — Question 18
You develop a database application for Microsoft SQL Server and Microsoft Azure SQL Database.
You need to raise an exception and transfer execution to a CATCH block.
You need to ensure that the exception returns output in the following format:
Msg 51000, Level 16, State 1, Line 1
The record does not exist.
Which Transact-SQL statement should you run?
Answer options
- A. DECLARE @Message NVARCHAR(2048); SELECT @Message = FORMATMESSAGE('The record does not exist.'); THROW 51000 , 1 , @Message
- B. THROW 51000 , 'The record does not exist.' , 1
- C. THROW ERROR_MESSAGE ('The record does not exist.'), 1
- D. THROW 51000 , FORMATMESSAGE('The record does not exist.') , 1
Correct answer: B
Explanation
The correct answer is B because it directly raises an exception with the specified message format and includes the appropriate error number. Options A, C, and D either do not format the message correctly or use functions that do not align with the required output structure.