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

发布于 2024-12-12 08:15:54 字数 1713 浏览 0 评论 0原文

我正在 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 模拟器执行该语句,命令 STRSQL,它会起作用。所以它看起来像是“运行 SQL 脚本”客户端中的一个错误

任何提示将不胜感激!

I'm creating a SQL function on System i V7R1:

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
;

I execute it with "Run SQL script" tool, which is part of the iSeries Navigator V7R1.
It works on another V7R1 server (using iSeries Navigator V5R4), but not in that one where I'm working now. It fails with this message:

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.

If I remove the FOR block, it works.

Moreover if I execute the statement with 5250 Emulator, command STRSQL, it works. So it seems like a bug in "Run SQL script" client.

Any hint will be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄司 2024-12-19 08:15:54

问题在于 FOR 语句。尽管文档声明如果未指定,则会生成唯一的游标名称,但查询分析器在 Cursor-name CURSOR FOR 为可选和必需时不一致。通过 IBM Access Navigator Run Scripts 实用程序提交的 SQL 需要它。

FOR 语法

括号也不正确,但有时它们会被接受(STRSQL、Navigator Run SQL 脚本),有时则不会(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

The issue is with the FOR statement. The query analyzer is inconsistent on when the cursor-name CURSOR FOR is optional and when it is required even though the documentation states if it is not specifified a unique cursor name is generated. SQL submitted via the IBM Access Navigator Run Scripts utility require it.

FOR syntax

The parenthesis are also incorrect but sometimes they are accepted (STRSQL, Navigator Run SQL Scripts) and sometimes they aren't (DBVisualizer/JDBC).

TIL there must be a different query analyzer running depending on the source of the query.

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

拒绝两难 2024-12-19 08:15:54

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

PTF group  Level  Text
SF99701        5  DB2 FOR IBM I

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

Given the tests made by @JamesA and me, I fear the problem can be in the Program Temporary Fix (PTF) that this server hasn't and the other ones have. Specifically, running WRKPTFGRP command, I can guess it probably misses this PTF group:

PTF group  Level  Text
SF99701        5  DB2 FOR IBM I

Unfortunately I can't try installing it now :(.

遥远的她 2024-12-19 08:15:54

在 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 |

In the session properties of your IDE change the Statement Separator field value from ; to | then reconnect your session. then use | instead of ;. this way you can run your statement or procedure or function.

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 |
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文