2016-01-12 68 views
0

論壇。DB2查詢令牌SMALLINT無效

我正在使用IBM System i版本7.1。

我在源代碼中遇到了以下合併語句問題,所以我將它複製到數據庫客戶端以利用「運行SQL腳本」功能。

與其替換聲明中的@Variables中的編碼,我想聲明局部變量,以便我可以按原樣測試聲明。

中增加了以下「聲明並設置」行,我得到以下錯誤:

declare @groupId smallint 
set @groupId = 99 

declare @groupName varchar(40) 
set @groupName = 'Sam' 

declare @groupId smallint

SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token SMALLINT was not valid. Valid tokens: DYNAMIC SENSITIVE ASENSITIVE INSENSITIVE. Cause . . . . . : A syntax error was detected at token SMALLINT. Token SMALLINT is not a valid token. A partial list of valid tokens is DYNAMIC SENSITIVE ASENSITIVE INSENSITIVE. This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token SMALLINT. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. -- If the error token is , correct the SQL statement because it does not end with a valid clause.

Processing ended because the highlighted statement did not complete >successfully

我已經嘗試添加分號每一行的結束和開始和結束語句還是沒有成功。

下面是整個聲明,我試圖執行:

declare @groupId smallint 
set @groupId = 99 

declare @groupName varchar(40) 
set @groupName = 'Sam' 


merge into database.table as t 
       using (values(cast(@groupId as smallint) 
       ,cast(@groupName as varchar(40)) 
       )) 
       as caz(group_id 
    , group_name 
    ) 
       on t.group_id = caz.group_id 
       when matched then update 
       set t.group_name = caz.group_name 
       when not matched then 
       insert ( group_id 
    , group_name 
    ) 
       values (caz.group_id 
    , caz.group_name 
    ); 

任何幫助表示讚賞。

請讓我知道,如果我可以提供任何信息。

+1

閱讀[手冊中的複合語句]的正確語法可能是一個好主意(http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzcompoundstmt.htm?lang= EN)。 – mustaccio

+0

不僅語法。關於DECLARE VARIABLE的[docs也這樣說](http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzdeclvar.htm?lang=en):「**調用**此語句只能嵌入到應用程序中「。 – user2338816

回答

0

我可能有一個答案在這裏找到:https://stackoverflow.com/a/4451159/2272357

CREATE OR REPLACE VARIABLE variableName VARCHAR(50); 
SET variableName = 'blah'; 
SELECT * FROM table WHERE column = variableName; 
DROP VARIABLE variableName; 

我還沒有驗證它成功地工作。不過,我認爲這可能是一個當地問題。

+0

在給定示例中編寫一個單獨的'SET'語句愚蠢;只需在'CREATE'中添加'DEFAULT'子句來分配值即可。驗證完成了嗎?如果這是成功的,那麼接受答案似乎是恰當的。 – CRPence