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-SQP
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:

Explanation:
Box 1: THROW 51000, ‘Warning: Credit limit isover 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.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
ok
0
15
UN EJEMPLO DE MICROSOFT LIGERAMENTE MODIFICADO PARA MI USO Y DEMOSTRACION:
DECLARE @ErrorMessage VARCHAR(1000)
DECLARE @ErrorHuman INT
BEGIN TRY
— RAISERROR with severity 11-19 will cause execution to
— jump to the CATCH block.
SET @ErrorHuman=’7SAUE83′
RAISERROR (‘Error raised in TRY block.’, — Message text.
16, — Severity.
1 — State.
);
END TRY
BEGIN CATCH
SELECT
@ErrorMessage = ERROR_MESSAGE()
— Use RAISERROR inside the CATCH block to return error
— information about the original error that caused
— execution to jump to the CATCH block.
RAISERROR (@ErrorMessage, 16,1 );
END CATCH;
0
0
THE ANSWER IS INCORRECT.
CREATE TABLE test.customer(
CustomerID INT NOT NULL PRIMARY KEY CLUSTERED,
CreditLimit MONEY CHECK(CreditLimit <7000)
)
CREATE TABLE test.errorLog(
ApplicationID INT NOT NULL,
Date DATETIME,
ErrorMessage VARCHAR(1000)
)
ALTER PROCEDURE test.UpdateCustomer
@CustomerID INT,
@CreditLimit MONEY
AS
BEGIN
DECLARE @ErrorMessage VARCHAR(1000)
BEGIN TRY
UPDATE test.customer
SET CreditLimit=@CreditLimit
where CustomerID=@CustomerID
END TRY
BEGIN CATCH
SET @ErrorMessage=ERROR_MESSAGE()
INSERT INTO test.errorLog(ApplicationID,Date,ErrorMessage)
VALUES (1,GETDATE(),@ErrorMessage)
RAISERROR(@ERRORMESSAGE,16,1)
END CATCH
END
EXEC test.UpdateCustomer 1,1999
EXEC test.UpdateCustomer 1,10999
0
0
query completed no errors,en serio esta dura esta pregunta
ALTER PROCEDURE test.UpdateCustomer
@CustomerID INT,
@CreditLimit MONEY
AS
BEGIN
DECLARE @ErrorMessage VARCHAR(1000)
BEGIN TRY
UPDATE test.cUstomer
SET CreditLimit=@CreditLimit
where CustomerID=@CustomerID
END TRY
BEGIN CATCH
SET @ErrorMessage=ERROR_MESSAGE()
–DECLARE @ErrorMessage varchar(250)
set @ErrorMessage=’error el limite de credito debe ser inferior a 10000′
INSERT INTO test.errorLog(ApplicationID,Date,ErrorMessage)
VALUES (1,GETDATE(),@ErrorMessage)
–RAISERROR(@ERRORMESSAGE,16,1)
–THROW 51000,’UPDATE en conflicto con la restricción CHECK!!!’,1
END CATCH
–will return the complete original error message as an error message
DECLARE @ErrorNumber int, @ErrorSeverity int, @ErrorState int, @ErrorLine int
SELECT @ErrorMessage = N’Error %d, Line %d, Message: ‘+ERROR_MESSAGE(),@ErrorNumber = ERROR_NUMBER(),@ErrorSeverity = ERROR_SEVERITY(),@ErrorState = ERROR_STATE(),@ErrorLine = ERROR_LINE()
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorNumber,@ErrorLine)
END
0
0
THE CORRECT ANSWER IS
ALTER PROCEDURE test.UpdateCustomer
@CustomerID INT,
@CreditLimit MONEY
AS
BEGIN
DECLARE @ErrorMessage VARCHAR(1000)
BEGIN TRY
UPDATE test.cUstomer
SET CreditLimit=@CreditLimit
where CustomerID=@CustomerID
END TRY
BEGIN CATCH
SET @ErrorMessage=ERROR_MESSAGE()
INSERT INTO test.errorLog(ApplicationID,Date,ErrorMessage)
VALUES (1,GETDATE(),@ErrorMessage)
RAISERROR(‘WARNING:CREDIT LIMIT IS OVER 7000’,16,1)
END CATCH
END
0
1
Where is the validation to perform this:
“Produces a warning if the credit limit parameter is greater than 7,000”
I think this answer is not valid and so is the code
however I would like to left few notes:
1)
if it is a warning its not supposed to break execution so it must be a RAISERROR used with severety below or equals to 10
2)
if you want to propagate an unexpected error then only option is ‘THROW’
22
0
But as per the following documentation “RAISERROR can’t be used in the Sql Server 2014’s Natively compiled Stored Procedures”: https://sqlhints.com/2013/06/30/differences-between-raiserror-and-throw-in-sql-server/. So it is not clear if we can use RAISERROR, as the requirement says that it is a stored procedure.
0
0
And possibly we need a semicolon before THROW.
1
0
need to add an if statement to tell if the credit limit parameter is greater than 7,000 and then raiserror…
0
0