2015-09-16 12 views
2

我试图表现出RAISERROR @MaxAmountint变量和@MinAmount如何使用变量在SQL RAISERROR

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

但是我得到错误:

Must declare the scalar variable "@MaxAmount".

+0

其基本的,你必须在查询中使用它之前声明变量。 –

+0

我确实声明了,但是当我在raiserror中使用它时出现了输入错误。解决了! – Nuke

回答

2

%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

我觉得你这样试试:

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

您需要使用%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)