【发布时间】: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