I have this stored procedure:
CREATE PROCEDURE [dbo].[get_test_status]
@CreatedBy INT OUTPUT,
@TestStatusId INT OUTPUT,
@UserTestId INT,
@UserId INT
AS
How can I throw an error if the @UserId parameter is a null or zero ?
Here's what I tried but it gives a syntax error:
IF (@UserId = 0 OR @UserId IS NULL)
THROW 70001, "UserId: Cannot be zero or null" , 1
I think you've missed the ; -colon
IF (@UserId = 0 OR @UserId IS NULL)
THROW 70001, 'UserId: Cannot be zero or null' , 1;
You can use RAISERROR
:
IF @UserId = 0 OR @UserId IS NULL
RAISERROR (N'This is message %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
-- The message text returned is: This is message number 5.
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