Search code examples
sqlsql-serverraiserror

How to use variables in SQL raiserror


I am trying to show my int variables in raiserror @MaxAmount and @MinAmount

Raiserror('Total Amount should be less than %s and Greater than %s',16,1,@MaxAmount,@MinAmount)

But Im getting error:

Must declare the scalar variable "@MaxAmount".


Solution

  • %s is used for varchar and your variable is of type int hence you need to try to use correct format specifier ie, %d

    DECLARE @MaxAmount int = 16;
    DECLARE @minAmount int = 1;
    Raiserror('Total Amount should be less than %d and Greater than %d',@MaxAmount,@MinAmount)
    

    Check RAISEERROR for details.