Oracle触发器错误 - 无效的标识符

时间:2014-03-30 23:05:45

标签: oracle plsql triggers syntax-error

我有以下表格。

create table player (
    player_id int,
    firstname varchar(32),
    lastname varchar(32),
    birthdate date,
    country varchar(3),
    constraint pk_player primary key (player_id)
);

create table team (
    team_id int, 
    name varchar(32),
    city_id int,
    manager_id int, 
    coach_id int,
    constraint pk_team primary key (team_id),
    constraint fk_team_city foreign key (city_id) 
        references city (city_id),
    constraint fk_team_manager foreign key (manager_id) 
        references manager (manager_id),
    constraint fk_team_coach foreign key (coach_id) 
        references coach (coach_id)
);

create table play_in_team (
    player_id int,
    team_id int,
    from_date date,
    to_date date,
    position varchar(5),
    squad_number int,
    is_captain char(1),
    constraint pk_play_in_team primary key (player_id, team_id, from_date),
    constraint fk_play_player foreign key (player_id) 
        references player (player_id),
    constraint fk_play_team foreign key (team_id) 
        references team (team_id)
);

create table violations (
    team_id int,
    violation_text varchar(200),
    constraint fk_vio_team foreign key (team_id) 
        references team (team_id)
);

我被分配了以下问题。

  

创建一个触发器,每次都会插入违规消息   团队在任何时候都有不止一名队长。违规   消息应包括短语“VIOLATION - Multiple Captains”,and   船长的名字。您可以在每个中插入一对船长名称   违规信息。

我在尝试编译触发器时一直遇到语法错误,希望有人能指出我正确的方向。现在,我的触发器被定义为......

CREATE OR REPLACE TRIGGER one_captain_check
    AFTER INSERT OR UPDATE OF player_id, team_id, from_date, to_date, is_captain ON play_in_team
    FOR EACH ROW
        WHEN(new.is_captain = '1')
    BEGIN
        MERGE INTO violations
        USING (
            SELECT
                play_in_team.team_id,
                ('VIOLATION - Multiple Captains - ' || existing_captain.firstname || ' ' || existing_captain.lastname || ', ' || new_captain.firstname || ' ' || new_captain.lastname) "violation_text"
            FROM play_in_team
            JOIN player existing_captain
                ON existing_captain.player_id = play_in_team.player_id
            JOIN player new_captain
                ON new_captain.player_id = :new.player_id
            WHERE
                play_in_team.team_id = :new.team_id AND
                play_in_team.player_id != :new.player_id AND
                play_in_team.is_captain = '1' AND
                -- overlapping date ranges from http://tonyandrews.blogspot.com/2010/06/sql-overlap-test.html
                NVL(:new.to_date, from_date) >= from_date AND
                :new.from_date <= NVL(to_date, :new.from_date)
        ) new_violations
        ON (violations.team_id = new_violations.team_id AND violations.violation_text = new_violations.violation_text)
        WHEN NOT MATCHED THEN
            INSERT VALUES(new_violations.team_id, new_violations.violation_text);
    END;
/

当我尝试运行它时,我在sqlplus中遇到以下错误。

  

LINE / COL ERROR

     
     

2/3 PL / SQL:忽略SQL语句

     

20/83 PL / SQL:ORA-00904:&#34; NEW_VIOLATIONS&#34;。&#34; VIOLATION_TEXT&#34;:无效标识符

我是Oracle和PL / SQL的新手,并且一直在尝试上述触发器的变体,但却无法进行编译。我使用的是Oracle Database 11g企业版11.2.0.4.0版。任何想法如何修复语法?

1 个答案:

答案 0 :(得分:2)

我没有在这里安装Oracle来测试它 - 但是请你不要把“violation_text”放在第10行的引号内,或者用引号标记大写?

Oracle默认情况下将不在引号内的每个小写更改为大写(对于表名和列名), 实际上你的专栏是new_violations。“violation_text”不等于new_violations .violation_text - 这是new_violations。“VIOLATION_TEXT”......