Ich nehme einen Datenbankkurs und nehme an, eine gespeicherte Prozedur für meine Datenbank in DB2 zu erstellen. Wir waren nur eine grundlegende Idee einer allgemeinen gespeicherten Prozedur gegeben und gesagt, die Syntax unter den Anbietern wie db2 variieren könnte, Postgres usw. So habe ich das folgende Verfahren schrieb mit, was wir in der Klasse gelernt:Erstellen und Ausführen einer gespeicherten Prozedur für ein SQL-Schema mit db2
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 = '[email protected]' WHERE O.ownerID = vownerID;
END IF;
FETCH C1 INTO vfranchID, vownerID, vname, vemail;
END WHILE;
CLOSE C1;
END
@
Ich habe gespeichert diese in einer Datei namens storedproc.sql
und versuchen, es auf dem Terminal zu kompilieren
db2 -t -f storedproc.sql
verwenden, aber ich bekomme die folgende
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.
Ich habe im Internet gesucht, um den gesamten Prozess der Einrichtung einer gespeicherten Prozedur zu finden, aber kein Glück. Ich verstehe nicht, wenn ich die falsche Syntax für meine Prozedur verwende, oder richte ich es falsch ein. Jede Hilfe wird sehr geschätzt. Vielen Dank :)
sehen, ob das hilft: http://stackoverflow.com/questions/21750346/db2-trigger-illegal-token/21755347#21755347 – mustaccio