2013-01-12 182 views
-1

我正在創建一個列,它會給我一個隨機數,但是我收到了一些錯誤。必須聲明一個標量變量

CREATE TABLE tblBookInfo 
(
AccessionNumber NVARCHAR(MAX) 
RandomNumber NVARCHAR(10); 
AccNo NVARCHAR(MAX) 
@Upper INT; 
@Lower INT 

SET @Lower = 1 
SET @Upper = 9999 

Select @RandomNumber = Round (((@Upper - @Lower -1)* RAND() + @Lower), 0) 

SET @AccNo = 'LIBBOOKS' + RandomNumber 
SELECT @AccNo 

Set AccessionNumber = AccNo 
) 

錯誤:

 
Incorrect syntax near 'RandomNumber'. 
Must declare the scalar variable "@Lower". 
Must declare the scalar variable "@Upper". 
Must declare the scalar variable "@Upper". 
Must declare the scalar variable "@AccNo". 
Must declare the scalar variable "@AccNo". 

感謝您的幫助:d

回答

1

它不是很清楚你想什麼來完成,但這裏是我在你的代碼的解釋正確T-SQL語法。 我不準備猜測你在下面試圖做什麼。

CREATE TABLE tblBookInfo 
( 
    AccessionNumber NVARCHAR(MAX), 
    RandomNumber NVARCHAR(10), 
    AccNo NVARCHAR(MAX) 
) 
DECLARE @Upper   INT, 
     @Lower   INT, 
     @RandomNumber INT 

SET @Lower = 1 SET @Upper = 9999 

SELECT @RandomNumber = Round (((@Upper - @Lower -1)* RAND() + @Lower), 0) 

SELECT @RandomNumber