使用SQL Server创建触发器时的列名无效

时间:2017-11-13 20:04:56

标签: sql sql-server

当我尝试使用SQL Server创建简单触发器时遇到了几个错误。表格使用:

创建
CREATE TABLE person 
(
    person_id INT NOT NULL PRIMARY KEY,
    name VARCHAR NOT NULL,
    phone INT,
    birth_date DATE,
    address VARCHAR
);

CREATE TABLE volunteers 
(
    person_id INT NOT NULL PRIMARY KEY,
    skill VARCHAR,
    FOREIGN KEY (person_id) REFERENCES person(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE physicians 
(
    person_id INT NOT NULL PRIMARY KEY,
    speciality VARCHAR,
    phone_number INT,
    FOREIGN KEY (person_id) REFERENCES person(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE patients  
(
    person_id INT NOT NULL PRIMARY KEY,
    contact_date DATE,
    physician_id INT,   -- TODO determine whether NULL able
                        -- TODO determine ON DELETE ACTION
    CHECK (person_id <> physician_id),
    FOREIGN KEY (person_id) REFERENCES person(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    FOREIGN KEY (physician_id) REFERENCES physicians(person_id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION
);

CREATE TABLE outpatients 
(
    person_id INT NOT NULL PRIMARY KEY,
    FOREIGN KEY (person_id) REFERENCES patients(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE visits 
(
    person_id INT  NOT NULL,
    date DATE NOT NULL,
    comments VARCHAR,
    PRIMARY KEY (person_id, date),
    FOREIGN KEY (person_id) REFERENCES outpatients(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE employees 
(
    person_id INT NOT NULL PRIMARY KEY,
    date_hire DATE,
    FOREIGN KEY (person_id) REFERENCES person(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE nurses 
(
    person_id INT NOT NULL PRIMARY KEY,
    certificate VARCHAR,
    care_center_name VARCHAR,
    FOREIGN KEY (person_id) REFERENCES employees(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    -- FOREIGN KEY (care_center_name) REFERENCES care_centers(name)
    --     ON DELETE CASCADE
    --     ON UPDATE CASCADE
);

CREATE TABLE care_centers 
(
    name VARCHAR PRIMARY KEY NOT NULL,
    nurses_in_charge INT NOT NULL,
    location VARCHAR,
    type VARCHAR
    -- FOREIGN KEY (nurses_in_charge) REFERENCES registered_nurse(person_id)
);

CREATE TABLE registered_nurse 
(
    person_id INT NOT NULL PRIMARY KEY,
    care_center_name VARCHAR,
    FOREIGN KEY (person_id) REFERENCES nurses(person_id),
        -- ON DELETE CASCADE
        -- ON UPDATE CASCADE,
    FOREIGN KEY (care_center_name) REFERENCES care_centers(name)
        -- ON DELETE CASCADE
        -- ON UPDATE CASCADE
);


ALTER TABLE nurses 
    ADD constraint nurses__care_centers_FK
    FOREIGN KEY (care_center_name) REFERENCES care_centers(name)
        ON DELETE CASCADE
        ON UPDATE CASCADE;

ALTER TABLE care_centers 
    ADD constraint care_centers__registered_nurse_FK
    FOREIGN KEY (nurses_in_charge) REFERENCES registered_nurse(person_id);

CREATE TABLE beds 
(
    bed_number INT NOT NULL,
    room_number INT NOT NULL,
    care_center_name VARCHAR,
    PRIMARY KEY (bed_number,room_number),
    FOREIGN KEY (care_center_name) REFERENCES care_centers(name)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE residents 
(
    person_id INT NOT NULL PRIMARY KEY,
    date_admitted DATE,
    bed_number INT,
    room_number INT,
    FOREIGN KEY (person_id) REFERENCES patients(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    FOREIGN KEY (bed_number, room_number) REFERENCES beds(bed_number, room_number)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE staffs 
(
    person_id INT NOT NULL PRIMARY KEY,
    job_class DATE,
    FOREIGN KEY (person_id) REFERENCES employees(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE technicians 
(
    person_id INT NOT NULL PRIMARY KEY,
    skill VARCHAR,
    FOREIGN KEY (person_id) REFERENCES employees(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
);

CREATE TABLE laboratories 
(
    name VARCHAR NOT NULL PRIMARY KEY,
    location VARCHAR
);

CREATE TABLE assignTechnicianToLab 
(
    person_id INT NOT NULL,
    laboratories_name VARCHAR NOT NULL,
    PRIMARY KEY(person_id, laboratories_name),
    FOREIGN KEY (person_id) REFERENCES employees(person_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    FOREIGN KEY (laboratories_name) REFERENCES laboratories(name)
        ON DELETE CASCADE
        ON UPDATE CASCADE
)

我试图创建的触发器:

-- Trigger
/* if nurse has certificate "RN", add to registered_nurse */
CREATE TRIGGER registered_nurse_trigger 
ON nurses
FOR INSERT, UPDATE
AS 
BEGIN
    IF EXISTS (SELECT * FROM inserted where certificate = "RN")
    BEGIN
        INSERT INTO registered_nurse 
        VALUES(inserted.person_id, nurses.care_center_name);
    END
END;

我一直在说错误:

  

Msg 207,Level 16,State 1,Procedure registered_nurse_trigger,Line 6   列名称“RN”无效。

     

Msg 4104,Level 16,State 1,Procedure registered_nurse_trigger,Line 8
  无法绑定多部分标识符“inserted.person_id”。

     

Msg 4104,Level 16,State 1,Procedure registered_nurse_trigger,Line 8
  无法绑定多部分标识符“nurses.care_center_name”。

我一直在这里待了几个小时,我真的很感激任何人都可以帮助我。非常感谢你!

2 个答案:

答案 0 :(得分:2)

你不能那样使用VALUES()。请改用INSERT..SELECT

-- Trigger
/* if nurse has certificate "RN", add to registered_nurse */
CREATE TRIGGER registered_nurse_trigger ON nurses
For INSERT, UPDATE
AS BEGIN
INSERT INTO registered_nurse (person_id, care_center_name)
SELECT person_id, care_center_name
FROM inserted
WHERE certificate = 'RN'
END;

我认为你不需要引用nurses或者EXISTS(),但也许我忽视了一些事情。

答案 1 :(得分:0)

您在创建触发器语句中使用双引号而不是单引号尝试此操作并查看它是否有效:

-- Trigger
/* if nurse has certificate "RN", add to registered_nurse */
CREATE TRIGGER registered_nurse_trigger ON nurses
    FOR INSERT, UPDATE
AS 
    BEGIN
        IF EXISTS (SELECT * FROM inserted where certificate = 'RN')
        BEGIN
            INSERT INTO registered_nurse 
                SELECT person_id, care_center_name FROM Inserted;
        END
    END;