在System i V7R1中创建函数时出错SQL0104

时间:2011-10-27 12:33:04

标签: sql for-loop ibm-midrange create-function db2-400

我在System i V7R1上创建了一个SQL函数:

CREATE FUNCTION MYSCHEMA.GROUPDIBAS(v_code VARCHAR(50))
RETURNS VARCHAR(2048)
LANGUAGE SQL
BEGIN
    DECLARE str VARCHAR(2048);
    SET str = '';
    FOR row AS (
        SELECT 
            FIELD2
        FROM MYSCHEMA.DIBAS
        WHERE FIELD1 = v_code
    )
    DO
        SET str = 'Bubi'; --I removed many statements to make clear the problem doesn't come from them
    END FOR;
    RETURN str;
END
;

我用"运行SQL脚本"执行它工具,它是iSeries Navigator V7R1的一部分。 它适用于另一台V7R1服务器(使用iSeries Navigator V5R4),但不适用于我现在正在使用的那台服务器。它失败了这条消息:

SQL State: 42601
Vendor Code: -104
Message: [SQL0104] Token <END-OF-STATEMENT> was not valid. Valid tokens: ;.
  Cause . . . . . :   A syntax error was detected at token <END-OF-STATEMENT>.
  Token <END-OF-STATEMENT> is not a valid token.  A partial list of valid tokens is ;.
  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 <END-OF-STATEMENT>. 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 <END-OF-STATEMENT>, correct the SQL statement because it does not end with a valid clause.

如果删除FOR块,则可以正常工作。

此外,如果我使用5250 Emulator执行语句,命令STRSQL,它可以工作。所以似乎是一个错误在&#34;运行SQL脚本&#34;客户端。

任何提示都将受到赞赏!

3 个答案:

答案 0 :(得分:4)

问题在于FOR statement。查询分析器在cursor-name CURSOR FOR何时是可选的以及何时需要时是不一致的,即使文档声明如果未指定,则生成唯一的游标名称。通过IBM Access Navigator Run Scripts实用程序提交的SQL需要它。

FOR syntax

括号也不正确但有时它们被接受(STRSQL,Navigator Run SQL Scripts),有时它们不是(DBVisualizer / JDBC)。

TIL必须运行不同的查询分析器,具体取决于查询的来源。

CREATE FUNCTION MYSCHEMA.GROUPDIBAS(v_code VARCHAR(50))
RETURNS VARCHAR(2048)
LANGUAGE SQL
BEGIN
    DECLARE str VARCHAR(2048);
    SET str = '';
    FOR row AS C1 CURSOR FOR
        SELECT 
            FIELD2
        FROM MYSCHEMA.DIBAS
        WHERE FIELD1 = v_code
    DO
        SET str = 'Bubi'; --I removed many statements to make clear the problem doesn't come from them
    END FOR;
    RETURN str;
END

query1

query2

答案 1 :(得分:1)

鉴于@JamesA和我进行的测试,我担心问题可能出现在程序临时修复(PTF)中,而该服务器没有,而其他服务器则没有。具体来说,运行WRKPTFGRP命令,我猜它可能会错过这个PTF组:

PTF group  Level  Text
SF99701        5  DB2 FOR IBM I

不幸的是我现在无法尝试安装它:(。

答案 2 :(得分:1)

在IDE的会话属性中,将Statement Separator字段值从;更改为|,然后重新连接会话。然后使用|代替;。这样你就可以运行你的陈述或程序或功能了。

usage example,
CREATE FUNCTION MYSCHEMA.GROUPDIBAS(v_code VARCHAR(50))
RETURNS VARCHAR(2048)
LANGUAGE SQL
BEGIN
    DECLARE str VARCHAR(2048);
    SET str = '';
    FOR row AS C1 CURSOR FOR
        SELECT 
            FIELD2
        FROM MYSCHEMA.DIBAS
        WHERE FIELD1 = v_code
    DO
        SET str = 'Bubi'; --I removed many statements to make clear the problem doesn't come from them
    END FOR;
    RETURN str;
END |