0

您好,我是 Oracle 数据库的新手并测试了一些功能。现在我有问题,而不是触发器。

这是我的代码:

    CREATE TABLE team(
    teamid integer PRIMARY KEY,
    team VARCHAR2(100)
);


CREATE TABLE player(
    playerid integer PRIMARY KEY,
    player VARCHAR2(100),
    position VARCHAR2(100),
    teamid integer REFERENCES team(teamid)
);


INSERT ALL
  INTO team (teamid, team) VALUES (1, 'FC Bayern Muenchen')
  INTO team (teamid, team) VALUES (2, 'Manchester United')
SELECT * FROM DUAL;


INSERT ALL
  INTO player (playerid, player, position, teamid) VALUES (1, 'Manuel Neuer', 'goalkeeper', 1)
  INTO player (playerid, player, position, teamid) VALUES (2, 'Dante', 'defense', 1)
  INTO player (playerid, player, position, teamid) VALUES (3, 'Cesc Fabregas', 'midfield', 2)
  INTO player (playerid, player, position, teamid) VALUES (4, 'Lionel Messi', 'striker', 2)
  INTO player (playerid, player, position, teamid) VALUES (5, 'Arjen Robben', 'midfield', 1)
SELECT * FROM DUAL;


CREATE VIEW view_player AS
SELECT p.playerid,p.player,p.position, t.team FROM player p
INNER JOIN team t
ON  p.teamid = t.teamid;

我得到了两个表的自动增量的序列和触发器。这是我的而不是触发器:

CREATE OR REPLACE TRIGGER view_player_insert
INSTEAD OF INSERT ON view_player
FOR EACH ROW
DECLARE
    NUMBER anzTeams;
    NUMBER teamID;
BEGIN
    SELECT count(*) INTO anzTeams FROM team WHERE team=:NEW.team;
    IF anzTeams > 0
    THEN
        INSERT INTO team(team) VALUES (:NEW.team);
    END IF;

    SELECT teamid INTO teamID FROM team WHERE team=:NEW.team;
    INSERT INTO player(player,position,fbid) VALUES (:NEW.player,:NEW.position,teamID);
END;

我用我的而不是触发器得到了这个异常:

第 12 行中的异常:PL/SQL:SQL 语句被忽略

我想我有一个语法错误,但我不知道是谁。我希望你能帮帮我 :)

Greetz Ulti

4

1 回答 1

0

更多评论(不能评论,双关语)。

  • 我不知道你可以有相同的列和表名。

  • 在声明子句中,变量名和类型的顺序可以互换。在最后一条语句中,将 fbid 更改为 teamID。通过这两个更改,触发器编译。我在第 12 行没有收到任何错误。

于 2013-09-11T14:56:36.703 回答