The INSERT INTO statement can be used to append a record to a table or to append multiple records from one table to another.
INSERT INTO MusicArtists (FirstName, LastName, Instrument)
VALUES ('Bobby', 'Lee', 'fiddle');
(1 row(s) affected)
To add a specific record to a table, the INSERT INTO statement is followed by the name of the table and the names of the various fields. Each value in the VALUES clause is inserted into the field that corresponds to the value's position in the list: the first value is inserted into the first field, the second into the second field etc.
Note: If you omit the field list, you must include a value for every field in the table, otherwise the operation will fail. The values must also be separated by commas. Text and date fields also must be enclosed in single quotation marks (' ').
An AutoNumber field (also referred to as a counter field) is a data type that automatically creates and stores a number for each item added to a table. If you append records to a table with an AutoNumber field and you do not want the AutoNumbered field to be renumbered, you must include the AutoNumber field number in the query. If you do not include the AutoNumber field, the appended items will be assigned a new AutoNumber field value.
INSERT INTO Duos (Member1)
SELECT FirstName + ' ' + LastName FROM MusicArtists;
(4 row(s) affected)
When using the multiple-record syntax the INSERT INTO statement precedes a SELECT statement and adds a single or multiple records to a table. This is referred to as an append query, as it copies records from one or more tables to another. The tables that contain the records being appended are not affected by the operation.
INSERT INTO Duos (Member1)
SELECT FirstName + ' ' + LastName FROM MusicArtists
WHERE MusicianID > 3;
(1 row(s) affected)
This example would append to the 'Duos' table, only those records that had a unique primary key.
INSERT INTO Residents (Name, Occupation)
SELECT Name, Occupation FROM Immigration
WHERE Residency = 'granted';
(5 row(s) affected)
This example demonstrates how to append records to a table from a subquery.
INSERT INTO Insurance (Name)
SELECT Employee.Username FROM Employee
INNER JOIN Project ON Employee.EmployeeID = Project.EmployeeID
WHERE Project.ProjectName = 'Hardwork';
(6 row(s) affected)
As with a straight-forward SELECT statement, the FROM clause may include more than one table linked by a JOIN operation. This is illustrated in this example, which appends to the 'Insurance' table the names of all those employees involved in the 'Hardwork' project.