如何在SQL raiserror中使用变量

时间:2015-09-16 10:03:23

标签: sql sql-server raiserror

我正在尝试在raiserror int@MaxAmount

中显示我的@MinAmount个变量
Raiserror('Total Amount should be less than %s and Greater than %s',16,1,@MaxAmount,@MinAmount)

但我得到错误:

  

必须声明标量变量" @ MaxAmount"。

4 个答案:

答案 0 :(得分:10)

%s用于varchar,而您的变量属于int类型,因此您需要尝试使用正确的格式说明符,即%d

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

查看RAISEERROR了解详情。

答案 1 :(得分:4)

你需要使用%I作为整数,如上所述,在使用前声明变量。

declare @MaxAmount int, @MinAmount int
select @MaxAmount = 50, @MinAmount = 5
Raiserror('Total Amount should be less than %i and Greater than %i',16,1,@MaxAmount,@MinAmount)

答案 2 :(得分:1)

我想你是这样尝试的:

DECLARE @MaxAmount int = 16;
DECLARE @MinAmount int = 1;

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

如果您想了解有关RAISERROR的更多信息,请go here

答案 3 :(得分:0)

上述解决方案对我不起作用,因为您还必须声明严重性和状态。没有它,结果将是这样的:

总金额应小于(空)且大于(空)

您可以尝试以下方法:

DECLARE @MaxAmount int = 16;
DECLARE @MinAmount int = 1;

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