我正在学习数据库课程,我想在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.
我一直在网上搜索,找到设置存储过程的整个过程,但没有运气。我不明白我的程序是否使用了错误的语法,或者我设置错了。任何帮助都感激不尽。谢谢:))
答案 0 :(得分:1)
DB2期望默认情况下以分号终止(完成)存储过程。您遵循良好做法并使用@
结束CREATE PROCEDURE
语句,现在您必须告诉DB2。
db2 -td@ -f storedproc.sql
-td@
告诉DB2使用@
作为语句终止符。此后,请确保文件中的所有语句都以该终结符结束。
您也可以在该文件中设置终结符。