Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AFAIK, for a trigger to be called on tblMatches, you need to supply values for all the columns in that table and only for the columns in that table which means, you cannot pass a team name to a trigger so that it can be used to resolve the team ID. </p> <p>If I were to do it, I would just create a table to insert the raw data as is, let the import wizard write records into this table, define a trigger on this table to insert records into the two derived tables viz. tblTeams, tblMatches. (<a href="http://msdn.microsoft.com/en-us/library/ms189799.aspx" rel="nofollow">SQL Server Triggers</a>)</p> <p>My attempt at writing one (didn't get a chance to verify as I don't have SQL server)</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload