sql Trigger:在插入之前从另一个表中选择FK

时间:2012-09-11 17:06:44

标签: sql sql-server-2008 tsql

我有两个数据库表。

  1. tblTeamsPK TeamIDTeamName
  2. tblMatchesPK match idFK HomeTeamFK AwayTeam,Score
  3. 我正在使用SQL Server 2008,我通过向导从.csv文件导入行。 csv中的列为hometeamawayteamscore。因此,在插入tblMatches之前,我想要一个触发器来查找团队的FK并在tblMatches中插入外键而不是名称。

    请提供任何帮助。

    CREATE TRIGGER tblmatches_BeforeInsert
    ON tblmatches
    BEFORE INSERT
    AS 
    BEGIN
        INSERT  tblmatches
        SELECT  teamName
        FROM    tblmatches
        WHERE   tblTeams.id = ?i dont know here what to insert? 
    END
    

2 个答案:

答案 0 :(得分:0)

AFAIK,对于要在tblMatches上调用的触发器,您需要为该表中的所有列提供值,并且仅为该表中的列提供值,这意味着,您无法将团队名称传递给触发器,以便它可以用于解析团队ID。

如果我这样做,我只会创建一个表来按原样插入原始数据,让导入向导将记录写入此表,在此表上定义一个触发器,将记录插入到两个派生表中即可。 tblTeams,tblMatches。 (SQL Server Triggers

我尝试编写一个(没有机会验证,因为我没有SQL服务器)

CREATE TRIGGER teams.process ON teams
AFTER INSERT
AS
BEGIN
    DECLARE @homeTeamId INT
    DECLARE @awayTeamId INT
    DECLARE @maxTeamId INT
    DECLARE @matchId INT

    SELECT @maxTeamId = 0
    SELECT @maxTeamId = ISNULL(MAX(teamId), 0) from tblTeams

    --- Check if home team has already been inserted into the table.
    SELECT @homeTeamId = -1
    SELECT 
        @homeTeamId = teamId 
    FROM 
        tblTeams t
        JOIN inserted i
        ON t.teamName = i.hometeam
    IF (@homeTeamId = -1) 
    BEGIN
        SELECT @homeTeamId = @maxTeamId + 1
        SELECT @maxTeamId = @maxTeamId + 1
        INSERT INTO tblTeams SELECT @homeTeamId, i.hometeam FROM inserted i
    END

    --- Check if away team has already been inserted into the table.
    SELECT @awayTeamId = -1
    SELECT 
        @awayTeamId = teamId 
    FROM 
        tblTeams t
        JOIN inserted i
        ON t.teamName = i.awayteam
    IF (@awayTeamId = -1) 
    BEGIN
        SELECT @awayTeamId = @maxTeamId + 1
        SELECT @maxTeamId = @maxTeamId + 1
        INSERT INTO tblTeams SELECT @awayTeamId, i.awayteam FROM inserted i
    END

    -- insert a record into the matches table with the home team ID and away team ID.
    SELECT @matchId = 0
    SELECT @matchId = ISNULL(MAX(MatchId), 0) FROM tblMatches
    INSERT INTO tblMatches 
    SELECT @matchId + 1, @homeTeamId, @awayTeamId, i.score 
    FROM inserted i
END 

答案 1 :(得分:0)

如果你想在表中使用不同的表示,那么你可能必须实现一个视图,并通过它来执行插入,而不是基表。

类似的东西:

CREATE TABLE realMatches (
    MatchID int IDENTITY(1,1) not null, /* Identity? */
    HomeTeamID int not null,
    AwayTeamID int not null,
    Score int not null, /* int? */
    constraint PK_realMatches PRIMARY KEY (MatchID),
    constraint FK_Matches_HomeTeams (HomeTeamID) references tblTeams (TeamID),
    constraint FK_Matches_AwayTeams (AwayTeamID) references tblTeams (TeamID)
)
GO
CREATE VIEW tblMatches
AS
    SELECT
        MatchID,
        ht.TeamName as HomeTeam,
        at.TeamName as AwayTeam,
        Score
    FROM
        realMatches m
           inner join
        tblTeams ht
           on
               m.HomeTeamID = ht.TeamID
           inner join
        tblTeams at
           on
               m.AwayTeamID = at.TeamID
GO
CREATE TRIGGER T_Matches ON tblMatches
INSTEAD OF INSERT
AS
     SET NOCOUNT ON

     INSERT INTO realMatches (HomeTeamID,AwayTeamID,Score)
     SELECT ht.TeamID,at.TeamID,i.Score
     FROM
        inserted i
           inner join
        tblTeam ht
           on
              i.HomeTeam = ht.TeamName
           inner join
        tblTeam at
           on
              i.AwayTeam = at.TeamName

您现在可以(假设“{1}}中存在”A Team“和”Team America“):

tblTeams

当然,这还没有(还)处理任何试图更改匹配表中的团队的更新,以及如果{{{ 1}},但你还没有问过这些。