2015-09-16 17 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)