I'm trying to write a trigger that deals with the following problem. I'm stuck trying to pull out the required variable from the inserted table, combining it with data from another table, and inserting that as one into required table (I know this is sort of vague/confusing, bear with me...)
This is how I want the database to work:
Three tables in the database:
- Table Users:
A table of user information, including an identifying number - Table PlaneGPSCoordinates:
A table of live GPS coordinates. This table never changes in size, the values are just updated. - Table MatchingInformation:
A table that has two columns, User ID number and plane coordinates.
The trigger should do the following:
- New row inserted into Table "Users" (i.e., a user signs up)
- Trigger automatically copies the data in PlaneGPSCoordinates (excluding a key column), and the new User's ID number, and inserts the two into Table MatchingInformation. There will be 1 user ID number for every 5 rows of Coordinates -> i.e., the trigger will add five rows to MatchingInformation, where userID = the new User's ID and coordinates are all copied from table PlaneGPSCoordinates.
Currently, my incomplete code looks like:
CREATE TRIGGER dbo.Matching
ON dbo.UserInfo
FOR INSERT
AS
DECLARE @userID as INT
BEGIN
SET NOCOUNT ON;
SELECT @userID = inserted.ID FROM inserted
INSERT....
END
GO
I'm very new to SQL, so assume the worst. I don't know how to now create the INSERT statement, where it pulls the GPS coordinates, and combines it with the user ID, to push that to the Matching table...
Any help is much appreciated!