语法错误 (-201) 为啥?

Posted

技术标签:

【中文标题】语法错误 (-201) 为啥?【英文标题】:Syntax error (-201) why?语法错误 (-201) 为什么? 【发布时间】:2014-03-26 02:29:52 【问题描述】:

为什么在这个 SPL 过程中出现语法错误?我得到的唯一消息是在服务器工作室中使用语法检查时存在一些错误。

create function najboljihN(n smallint)
returning char(10) as jmbag, char(50) as prezime, char(50) as ime, decimal(3,2) as prosjek;

   define jmbag like student.jmbag;
   define prezime like student.prezime;
   define ime like student.ime;
   define prosjek decimal(3,2);
   define i integer;

  for i=1 to n step 1
 SELECT student.jmbag, 
   student.prezimestudent, 
   student.imestudent, 
   Avg(ocjena) 
 INTO   jmbag, prezime, ime, prosjek 
FROM   student 
   JOIN upisanpredmet 
     ON student.jmbag = upisanpredmet.jmbag 
GROUP  BY student.jmbag, 
      student.prezimestudent, 
      student.imestudent 
 ORDER  BY ocjena, 
      student.prezimestudent asc, 
      student.imestudent asc; 

 RETURN jmbag, prezime, ime, ocjena WITH RESUME;
 end for;

end function;

【问题讨论】:

在我熟悉的任何 DB2 平台(z/OS、LUW、iSeries)的信息中心页面上,我都找不到任何提及 -201 错误的信息...你确定吗这就是你收到的信息?你能粘贴你得到的错误的全文吗? @bhamby SPL 是 Informix 编程语言; DB2 标签不属于这里。 @mustaccio 是有道理的,那么。 :) 【参考方案1】:

在这种情况下,您不能使用 order by 子句。如果没有 order by 子句,您可以编译您的函数。

create function najboljihN(n smallint)
     returning char(10) as jmbag, char(50) as prezime, char(50) as ime, decimal(3,2) as prosjek;

        define jmbag like student.jmbag;
        define prezime like student.prezime;
        define ime like student.ime;
        define prosjek decimal(3,2);
        define i integer;

        for i = 1 to n step 1
      SELECT jmbag, 
        prezimestudent, 
         imestudent, 
        Avg(ocjena) 
      INTO   jmbag, prezime, ime, prosjek 
     FROM   student 
        JOIN upisanpredmet 
          ON student.jmbag = upisanpredmet.jmbag 
     GROUP  BY student.jmbag, 
           student.prezimestudent, 
           student.imestudent; 
     -- ORDER  BY ocjena, 
     --      student.prezimestudent asc, 
     --      student.imestudent asc; 

      RETURN jmbag, prezime, ime, prosjek WITH RESUME;
      end for;

     end function;   

我不完全理解您对循环的意图,因为您执行 n 次相同的 sql。但如果这真的是您的意图,您可以将结果写入临时表,然后从临时表中返回您的值。

CREATE FUNCTION najboljihN(n SMALLINT)
   RETURNING CHAR(10) AS jmbag, CHAR(50) AS prezime, CHAR(50) AS ime, DECIMAL(3, 2) AS prosjek;

   DEFINE jmbag LIKE student.jmbag;
   DEFINE prezime LIKE student.prezime;
   DEFINE ime LIKE student.ime;
   DEFINE prosjek DECIMAL(3, 2);
   DEFINE i INTEGER;

 -- CREATE Temp Table    
   CREATE TEMP TABLE t1(
            jmbag     CHAR(10),
            prezime   CHAR(50),
            ime       CHAR(50),
            prosjek   DECIMAL(3, 2))

 -- Fill Temp Table
   FOR i = 1 TO n step 1 

INSERT INT t1
       SELECT jmbag, prezimestudent, imestudent, Avg(ocjena) as prosjek
       FROM student JOIN upisanpredmet ON student.jmbag = upisanpredmet.jmbag
       GROUP BY student.jmbag, student.prezimestudent, student.imestudent;

   END FOR;

 -- Return from temp table
   FOREACH
       SELECT jmbag, prezimestudent, imestudent, prosjek 
    INTO jmbag, prezime, ime, prosjek
       FROM t1 ORDER BY prosjek

       RETURN jmbag, prezime, ime, prosjek WITH RESUME;

   END FOREACH;

END FUNCTION;

【讨论】:

tnx :) 但是你能给我链接到 literatore 或者解释一下为什么 order by cause 语法错误吗?在上面的这种情况下,我该如何排序,以免出错? @Error,我为您的排序问题添加了解决方案。【参考方案2】:

Informix manuals says:

ORDER BY 子句意味着查询返回不止一行。 在 SPL 中,如果您指定 ORDER,数据库服务器会发出错误 BY 子句没有 FOREACH 循环来处理返回的行 单独在 SPL 例程中。

【讨论】:

以上是关于语法错误 (-201) 为啥?的主要内容,如果未能解决你的问题,请参考以下文章

Informix 错误:发生了语法错误。错误代码:-201

为啥 erlang spawn 函数调用中出现语法错误 - “之前的语法错误:')'”?

为啥cad安装会出现命令行语法错误

为啥我会收到此语法错误? (Python)

我不明白为啥会出现语法错误

为啥运行 yarn start 时出现语法错误