exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 182 discussion

Actual exam question from Microsoft's 70-761
Question #: 182
Topic #: 1
[All 70-761 Questions]

DRAG DROP -
You need to create a stored procedure that meets the following requirements:
✑ Produces a warning if the credit limit parameter is greater than 7,000
✑ Propagates all unexpected errors to the calling process
How should you complete the Transact-SQL statement? To answer, drag the appropriate Transact-SQL segments to the correct locations. Each Transact-SQL segments may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:
Box 1: THROW 51000, 'Warning: Credit limit is over 7,000!",1
THROW raises an exception and transfers execution to a CATCH block of a TRY"¦CATCH construct in SQL Server.
THROW syntax:
THROW [ { error_number | @local_variable },
{ message | @local_variable },
{ state | @local_variable } ]
[ ; ]
Box2: RAISERROR (@ErrorMessage, 16,1)
RAISERROR generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated
CATCH block of a TRY"¦CATCH construct. New applications should use THROW instead.
Severity levels from 0 through 18 can be specified by any user. Severity levels from 19through 25 can only be specified by members of the sysadmin fixed server role or users with ALTER TRACE permissions. For severity levels from 19 through 25, the WITH LOG option is required.
On Severity level 16. Using THROW to raise an exception
The following example shows how to use the THROW statement to raise an exception.

Transact-SQL -
THROW 51000, 'The record does not exist.', 1;
Here is the result set.
Msg 51000, Level 16, State 1, Line 1
The record does not exist.
Note: RAISERROR syntax:
RAISERROR( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
Note: The ERROR_MESSAGE function returns the message text of the error that caused the CATCH block of a TRY"¦CATCH construct to be run.
References:
https://msdn.microsoft.com/en-us/library/ms178592.aspx
https://msdn.microsoft.com/en-us/library/ms190358.aspx
https://msdn.microsoft.com/en-us/library/ee677615.aspx

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
M4x
Highly Voted 5 years, 7 months ago
From Microsoft. If you raise an error that has a severity level of 10 or less, which is a warning, no exception is raised. https://support.microsoft.com/en-ca/help/321903/how-to-return-errors-and-warnings-from-a-sql-server-stored-procedure-i Correct answer for first box RAISEERROR ('..', 10, 1)
upvoted 13 times
anonimdom
5 years, 3 months ago
"10 or less" means: if severity level is 10, then no exception is raised. So severity should be 11 or more.
upvoted 1 times
anonimdom
5 years, 2 months ago
Sorry, it looks like I misunderstood the question
upvoted 1 times
...
...
...
AshleyLiang
Highly Voted 5 years, 8 months ago
As the first requirement is to produce a warning and the second is an error, the severity in RAISERROR() should be 0-10 and 11-19 respectively. Severity 20-25 is fatal and terminates the current connection.
upvoted 8 times
...
Billybob0604
Most Recent 4 years, 3 months ago
Anette's answer is right.. Just tested it : If you uncomment the select 1/0, you see that the catch block is reached. This is the 'unexpected error' type. Throw does not work anywhere in this case drop table #test create table #test ( custid varchar(5) not null ,income money ) if object_ID('abort_test','P') IS NOT NULL drop proc abort_test go create proc abort_test as begin declare @creditlimit int = 8000 declare @errormessage varchar(1000) begin try if @creditlimit > 7000 print 'Preparing for warning in try block............' raiserror ('Warning: Credit limit is over 7,000', 10 ,1) insert into #test (custid, income) values ('A5666', 20000) --select 1/0 end try begin catch print 'Preparing for raiserror in catch block............' set @errormessage = error_message() raiserror (@errormessage, 16,1) end catch; end set xact_abort OFF; exec abort_test
upvoted 1 times
...
Billybob0604
4 years, 3 months ago
For raiserror you have to generate an error of the appropriate severity, an error at severity 0 thru 10 do not cause you to jump to the catch block, so either raiserror or throw without the variable in it and with a severity level of 16 (which throw has by default) would do in the TRY block.
upvoted 1 times
...
Vermonster
4 years, 4 months ago
Since you need to raise a warning --> RAISERERROR 10-15 severity Propogate to calling code -> THROW RAISEERROR ('..', 10, 1) -> to raise the warning THROW 51000, @ErrorMessage, 1 -> to 'Propagates all unexpected errors ..
upvoted 1 times
...
Aghie
4 years, 8 months ago
i think the answers are: 1 box: RAISEERROR ('..', 10, 1) -> to raise the warning 2 box: THROW 51000, @ErrorMessage, 1 -> to 'Propagates all unexpected errors ..'
upvoted 5 times
...
Backy
4 years, 10 months ago
The req "Propagates all unexpected errors to the calling process" eliminates all choices with RAISERROR() for second target because all hardcode the severity, and you should get the severity by calling ERROR_SEVERITY(). Since @ErrorMessage is defined you have to use it, so the only possibility is THROW 51000, @ErrorMessage, 1 Since THROW is used for second target you should also use THROW for the first target and the best choice is THROW 51000, 'Warning: Credit limit is over 7000', 1 so the answer should be THROW 51000, 'Warning: Credit limit is over 7000', 1 THROW 51000, @ErrorMessage, 1
upvoted 1 times
...
Anette
4 years, 11 months ago
Correct solution: 1. RAISERROR('Warning: Credit limit is over 7,000!', 10,1) 2. RAISERROR('Warning: Credit limit is over 7,000!', 16,1) Tested :)
upvoted 6 times
Anette
4 years, 11 months ago
sorry second box does not have any sense but throw fits here. Even just throw or throw 51000, @ErrorMessage, 1
upvoted 1 times
Anette
4 years, 11 months ago
But I would go with just Throw, since it gives the error code from SQL server and it might not only be 51000 but depends from type of error which occurs. So, Second box is just Throw in my opinion.
upvoted 1 times
Anette
4 years, 11 months ago
SORRY I see the semicolon here is the problem for Throw. If we use Throw it might need ; before and since we do not have it, We are obliged to go with raiseerror, and the valuable option here is RAISERROR (@errormessage, 16, 1). This is FINAL for BOx 2.
upvoted 5 times
...
...
...
...
Andi64
5 years, 2 months ago
<Produces a warning if the credit limit parameter is greater than 7,000> : This doesn't mean that the credit limit can't be inserted into the table, it's just a warning, therefore the 1st Statmente should be Raiserror ('Warning.... ', 10,1). Only on a runtime error the procedure have to walk into the the Catch block, log the errormessage and probagate the error status to the calling process. Just the "THROW;" without paramenters seems to be enough as second statement.
upvoted 6 times
xd1
5 years ago
no semi colon before throw though does not work then I think
upvoted 1 times
xd1
5 years ago
makes sense to use raiserror here as you capture the error for a reason into your variable
upvoted 1 times
...
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago