0
ALTER PROCEDURE sp1
as
begin
IF (@PersonId=0)
    RAISERROR(N'Error already raised. See previous errors.', 16, 1);
    break
end

I want something like the following.

If no value in the variable, how to break the procedure in SQL Server?

1 Answer 1

1

If you want to raise error and exit from the procedure, you may want to try return. Example below.

alter PROCEDURE sp1
@PersonID int
as
begin
   IF (@PersonId is null)  --you can check 0, if that is the intention
   begin
       RAISERROR(N'Error already raised. See previous errors.', 16, 1);
       return;
   end;
   print '@PersonId is not null';  --Condition when error is not raised
end;
1
  • From a stored procedure you can return an integer value e.g. return -3;, which can be interrogated by the caller to take appropriate action. SPs also support OUT parameters for returning values to the caller. Commented Jul 27, 2014 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.