【问题标题】:How to create and run a stored procedure for a sql schema with db2如何使用 db2 为 sql 模式创建和运行存储过程
【发布时间】:2016-03-22 12:41:00
【问题描述】:

我正在学习数据库课程,我想在 DB2 中为我的数据库创建一个存储过程。我们刚刚获得了通用存储过程的基本概念,并告诉我们语法可能因 db2、postgres 等供应商而异。 所以我用我们在课堂上学到的东西编写了以下程序:

Connect to cs***;
CREATE PROCEDURE FRANCHISE_INFO (IN franchID INTEGER)
    LANGUAGE SQL
BEGIN
    DECLARE at_end INTEGER DEFAULT 0;
    DECLARE vfranchID INTEGER;
    DECLARE vownerID INTEGER; 
    DECLARE vname VARCHAR(25);
    DECLARE vemail VARCHAR(30); 
    DECLARE not_found CONDITION FOR SQLSTATE '02000';

    DECLARE C1 CURSOR FOR 
        SELECT F.franchiseID, O.ownerID, O.name, O.email
        FROM Franchise F, Owner O
        WHERE F.franchiseID = franchID AND F.ownerID = O.ownerID;
        DECLARE CONTINUE_HANDLER FOR not_found SET at_end = 1;
        OPEN C1;
        FETCH C1 INTO vfranchID, vownerID, vname, vemail;
        WHILE @at_end = 0 DO
            IF(O.name == NULL)
                THEN UPDATE owner SET O.name = 'R McDonald' WHERE O.ownerID = vownerID; 
            END IF;
            IF(O.email == NULL)
                THEN UPDATE owner SET O.email = 'headoffice@mcdonald.ca' WHERE O.ownerID = vownerID;
            END IF;
            FETCH C1 INTO vfranchID, vownerID, vname, vemail;
        END WHILE;
        CLOSE C1;
END 
@

我已将其保存在一个名为 storedproc.sql 的文件中,并尝试在终端上使用

db2 -t -f storedproc.sql

但我得到以下内容

Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.3
 SQL authorization ID   = CS******
 Local database alias   = CS***


DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "nd 
INTEGER DEFAULT 0".  Expected tokens may include:  "<psm_semicolon>".  LINE 
NUMBER=4.  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "INTEGER" was found following "DECLARE vfranchID 
".  Expected tokens may include:  "END-OF-STATEMENT".  LINE NUMBER=1.  
SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "INTEGER" was found following "DECLARE vownerID 
".  Expected tokens may include:  "END-OF-STATEMENT".  LINE NUMBER=1.  
SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "DECLARE vname VARCHAR" was found following 
"BEGIN-OF-STATEMENT".  Expected tokens may include:  "<compile_fragment>".  
LINE NUMBER=1.  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "DECLARE vemail VARCHAR" was found following 
"BEGIN-OF-STATEMENT".  Expected tokens may include:  "<compile_fragment>".  
LINE NUMBER=1.  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "'02000'" was found following "NDITION FOR 
SQLSTATE".  Expected tokens may include:  "END-OF-STATEMENT".  LINE NUMBER=1.  
SQLSTATE=42601

DB21031E  The SQL statement using the cursor "C1" ("SQLCUR1") returned:
SQL0206N  "FRANCHID" is not valid in the context where it is used.  
SQLSTATE=42703

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "<space>" was found following 
"CONTINUE_HANDLER".  Expected tokens may include:  "FOR".  LINE NUMBER=1.  
SQLSTATE=42601

DB21028E  The cursor "C1" has not been declared.

SQL0104N  An unexpected token "INTO" was found following "<identifier>".  
Expected tokens may include:  "END-OF-STATEMENT".  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "WHILE" was found following 
"BEGIN-OF-STATEMENT".  Expected tokens may include:  "<variable_set>".  
SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "END IF". 
Expected tokens may include:  "JOIN <joined_table>".  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "=" was found following "IF(O.email =".  
Expected tokens may include:  "<space>".  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "END IF". 
Expected tokens may include:  "JOIN <joined_table>".  SQLSTATE=42601

SQL0104N  An unexpected token "INTO" was found following "<identifier>".  
Expected tokens may include:  "END-OF-STATEMENT".  SQLSTATE=42601

DB21034E  The command was processed as an SQL statement because it was not a 
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "END-OF-STATEMENT" was found following "END 
WHILE".  Expected tokens may include:  "JOIN <joined_table>".  SQLSTATE=42601

DB21028E  The cursor "C1" has not been declared.


DB21007E  End of file reached while reading the command.

我一直在网上搜索,想找到设置存储过程的整个过程,但没有运气。我不明白我是否在我的程序中使用了错误的语法,或者我是否设置错误。任何帮助都感激不尽。谢谢你:)

【问题讨论】:

标签: sql stored-procedures db2 db2-luw


【解决方案1】:

默认情况下,DB2 期望存储过程以分号终止(完成)。您遵循了良好的做法并使用@ 来结束CREATE PROCEDURE 语句,现在您必须告诉DB2。

db2 -td@ -f storedproc.sql

-td@ 告诉 DB2 使用 @ 作为语句终止符。此后确保文件中的所有语句都以该终止符结尾。

您还可以在该文件中设置终止符。

【讨论】:

  • 仍然收到错误[cs421***][comp***][~] db2 -td@ -t storedproc.sql DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0104N An unexpected token "END-OF-STATEMENT" was found following "storedproc.sql". Expected tokens may include: "JOIN &lt;joined_table&gt;". SQLSTATE=42601
  • 你有“-td@ -t”,去掉第二个“-t”
  • [cs421***][comp***][~] db2 -td@ -f storedproc.sql SQL0104N An unexpected token "CREATE" was found following "&lt;identifier&gt;". Expected tokens may include: "USER". SQLSTATE=42601 现在得到以上内容,有什么建议吗?
  • 您正在混合终止符:connect 使用分号,create procedure 使用@。将connect 更改为以@ 结尾
  • @SSRed -- 您是否偶然尝试在某种 GUI 客户端中将操作系统命令 db2 作为 SQL 语句运行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
  • 2021-08-03
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
相关资源
最近更新 更多