I have this code:
IF (@CurrentResult != 'N' AND @CurrentResult != 'F')
THROW 50002, 'Question should be unmarked or incorrect', 1
Can someone explain to me how I can also give the @CurrentResult in the error message I throw?
What I would like is for the message to appear like:
Question result is 'X'. It should be unmarked or incorrect?
Where X is the value of @CurrentResult.
You cannot concatenate strings inside a THROW
statement.
What you need to do is create a variable and assign your whole error message to it:
DECLARE @Message NVARCHAR(100)
SET @Message = N'Question result is ''' + @CurrentResult + '''. It should be unmarked or incorrect?'
IF (@CurrentResult != 'N' AND @CurrentResult != 'F')
THROW 50002, @Message, 1
Edited to reflect setting message variable first:
DECLARE @Message varchar(100)
SET @Message = 'Question result is ' + cast(@CurrentResult, varchar(100)) + '. It should be unmarked or incorrect.'
IF (@CurrentResult != 'N' AND @CurrentResult != 'F')
THROW 50002,@Message,1
Casting to varchar will keep the text value of whatever @CurrentResult
is. Set the size accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With