在多个表视图上插入带有检查选项的触发器

时间:2015-01-21 18:16:19

标签: sql sql-server tsql

我在多个表视图上遇到触发器问题。

让我解释一下: 我有2个数据表,主键和表事件有很多关系。

CREATE TABLE Club
(
    name_of_club varchar(50) PRIMARY KEY,
    adress varchar(50) NOT NULL
)

CREATE TABLE Band
(
    name_of_band varchar(50) PRIMARY KEY,
    members int NOT NULL
)


CREATE TABLE Event
(
    name_of_club varchar(50) NOT NULL,
    name_of_band varchar(50) NOT NULL,
        data date NOT NULL,
    CONSTRAINT PK_Event PRIMARY KEY
    (
        name_of_club,
        name_of_band
    ),
    FOREIGN KEY (name_of_club) REFERENCES Club (name_of_club),
    FOREIGN KEY (name_of_band) REFERENCES Band (name_of_band)
)

现在我可以添加一些值:

insert into Club (name_of_club, adress)
       values
       ('Big London Club', 'Londyn, Baker Street 12'),
       ('Espana Time', 'Barcelona, Diego 15'),
       ('Wwa', 'Warszawa, Sezonowa 82/14'),
       ('Szwalnia', 'Łódź, Piotrkowska 20/2');

insert into Band (name_of_band, members)
       values
       ('Arctic Monkeys', 5),
       ('Random Guys', 3),
       ('TILT', 10),
       ('Joke Time', 2);



insert into Event (name_of_club, name_of_band, data)
           values
           ('Big London Club', 'Arctic Monkeys', '2015-01-25'),
           ('Wwa', 'TILT', '2015-01-28'),
           ('Szwalnia', 'Joke Time', '2015-02-10');

让我们创建视图:

CREATE VIEW Events AS

SELECT Club.name_of_club, adress, Band.name_of_band, members, data
FROM Event
JOIN Club ON Club.name_of_club = Event.name_of_club
JOIN Band ON Band.name_of_band = Event.name_of_band 

现在我必须为INSERT操作创建一个触发器。

1 *在视图中插入行时,我需要检查是否存在行:

  • 是 - >显示错误

  • 否 - >转到第2点

2 *检查插入时是否有俱乐部和带有指定名称的乐队。

  • 如果是,请检查信息是否正确 纠正并转到3 *。
  • 如果出现问题 - >显示错误。

3 *当该行包含俱乐部的数据且该乐队不在时    表,化妆基表。在最后化妆事件表    代表多对多的关系。现在应该会更好。

编辑:触发点工作

2 个答案:

答案 0 :(得分:1)

可以使用INSTEAD OF TRIGGER来完成,触发器应该是这样的:

CREATE TRIGGER InsteadOfEventInsert ON Events INSTEAD OF INSERT
AS
BEGIN
    DECLARE @name_of_club VARCHAR(50),
            @name_of_band VARCHAR(50),
            @data DATE,
            @members int

    SELECT  @name_of_club = name_of_club,
            @name_of_band = name_of_band,
            @data = data,
            @members = members
    FROM INSERTED

    IF NOT EXISTS(  SELECT  1 
                    FROM    Events 
                    WHERE   name_of_club = @name_of_club 
                    AND     name_of_band = @name_of_band 
                    AND     data = @data
                )
    BEGIN
        IF NOT EXISTS(SELECT 1 FROM Club WHERE name_of_club = @name_of_club)
            INSERT INTO Club VALUES(@name_of_club, @members)

        IF NOT EXISTS(SELECT 1 FROM Band WHERE name_of_band = @name_of_band)
            INSERT INTO Band VALUES(@name_of_band, @members)

        INSERT INTO [Event] SELECT name_of_club, name_of_band, data FROM INSERTED
    END
END

我建议只为Club和Band添加一个INT标识列,而不是将名称作为主键。

答案 1 :(得分:0)

示例插入:

INSERT INTO Events values ('Test Club', 'Test Adress', 'Test Band', 5, '2015-01-01')

触发:

CREATE TRIGGER InsteadOfEventInsert ON Events INSTEAD OF INSERT
AS
BEGIN
    DECLARE @name_of_club VARCHAR(50),
            @adress varchar(50),
            @name_of_band VARCHAR(50),
            @members int,
            @data date

    SELECT  @name_of_club = name_of_club,
            @adress = adress,
            @name_of_band = name_of_band,
            @members = members,
            @data = data
    FROM INSERTED

    IF NOT EXISTS(  SELECT  1 
                    FROM    Events 
                    WHERE   name_of_club = @name_of_club 
                    AND     adress = @adress
                    AND     name_of_band = @name_of_band 
                    AND     data = @data
                )

    BEGIN
        IF NOT EXISTS(SELECT 1 FROM Club WHERE name_of_club = @name_of_club)
        INSERT INTO Club VALUES(@name_of_club, @adress)

        IF NOT EXISTS(SELECT 1 FROM Band WHERE name_of_band = @name_of_band)
        INSERT INTO Band VALUES(@name_of_band, @members)
    END
    INSERT INTO [Event] SELECT name_of_club, name_of_band, data FROM INSERTED
END

看起来应该是这样的吗?

编辑:它有效;)