Search code examples
sqlstored-proceduressap-aseraiserror

Formatting RAISERROR messages in SYBASE ASE 12.5


In a stored procedure, I am trying to test if a parameter is null or less then 1, and if so, raise an error with a friendly message, and the value of the parameter.

Here is my code:

IF @ID IS NULL OR @ID <= 0
BEGIN
    RAISERROR 27001 'ID is ?????. ID cannot be null or less then zero.'
    RETURN 27001
END

What I would like back is either:

"ID is 0. ID cannot be null or less then zero." or "ID is null. ID cannot be null or less then zero."

I've tried building a varchar and raising the error like this:

RAISERROR 27001 @message

but that produces the error "Message Number 27001, passed to RAISERROR, does not exist in the sysmessages catalog."


Solution

  • This works, although an @ID value of NULL writes out as empty string.

    IF @ID IS NULL OR @ID <= 0
    BEGIN
    
        DECLARE @msg varchar(250)
        SELECT @msg = 'ID is ' +  Convert(varchar(15), @ID) + '. Value cannot be null or less then zero'
    
        RAISERROR 27001 @msg
        RETURN 27001
    END