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

问题描述 投票:0回答:1

我有以下表格。

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”和船长的姓名。您可以在每个违规消息中插入一对队长名称。

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

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:“NEW_VIOLATIONS”。“VIOLATION_TEXT”:标识符无效

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

oracle plsql triggers syntax-error
1个回答
2
投票

我没有在这里安装Oracle来测试它 - 但是你能不能将“violation_text”放在第10行的引号内,或者在引号内用大写字母表示吗?

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

© www.soinside.com 2019 - 2024. All rights reserved.