Skip to content

INSERT

Adds one or more rows to a table, from values you list or from the rows a query returns.

Access SQL, with extensions. ACE accepts the single-row VALUES form and the INSERT … SELECT form (the append query). A VALUES list of several rows and a UNION as the source are LibRed extensions, marked where they are described.

INSERT INTO table_name [ ( column_name [ , … ] ) ] [ IN external_database ]
VALUES ( { expression | DEFAULT } [ , … ] ) [ , ( … ) … ]
INSERT INTO table_name [ ( column_name [ , … ] ) ] [ IN external_database ]
query_expression
INSERT INTO table_name DEFAULT VALUES

The table to add rows to.

A column to fill. Columns you leave out take their default value, or Null if they have none. An AutoNumber column you leave out gets its next number.

Without a column list, a VALUES row fills every column of the table, in order. A query without a column list fills columns by name, not by position: each output column goes to the table column of the same name, so give each one an alias that matches. A query output name the table does not have is an error.

Appends to table_name in another Access database file instead of this one. The source rows (the VALUES or the query) are still read from this database, unless the query’s own FROM has an IN of its own. The IN goes after the column list; before the list it is a syntax error, as in ACE.

external_database takes the same forms as in a FROM clause: IN 'path', IN 'path' 'MS Access;', IN 'path' [MS Access;] or IN '' [;DATABASE=path]. Only Access databases are opened; a dBASE, Paradox, Excel or text type is refused.

The values of one row, one per target column, in the same order. Each value is an expression, NULL, or DEFAULT.

LibRed extension: several rows, separated by commas: VALUES (…), (…), (…). Access has only the single-row form. Every row must have as many values as there are target columns; a row that doesn’t fails the whole statement. There is no limit on the number of rows.

As a value in a VALUES row: the column’s declared default, or Null if it has none. This differs from NULL, which stores Null even when the column has a default. DEFAULT is accepted only in an INSERT’s VALUES.

A SELECT whose rows are appended. Its WHERE, ORDER BY and TOP apply as in any query.

LibRed extension: the source may be a set operation, such as SELECT … UNION ALL SELECT …. Access documents the source as a single SELECT.

Inserts one row in which every column takes its default (or Null), and the AutoNumber column its next number. EF Core emits this for an entity whose values are all generated by the database.

The statement reports the number of rows it added, which SELECT @@ROWCOUNT also returns afterwards. SELECT @@IDENTITY returns the last AutoNumber the statement generated; after a multi-row insert, that is the last row’s. After an insert with IN, it is the other file’s new AutoNumber. See System variables.

You can give an AutoNumber column a value of your own. The next generated number then continues after it.

An INSERT … SELECT that reads the table it appends to reads all its source rows first, so appending a table to itself doubles it and stops.

The statement is checked against the table’s rules, and fails as a whole if any row breaks one:

  • A NOT NULL column that ends up with no value fails, whether you left it out, gave it NULL, or used DEFAULT on a column with no default.
  • A duplicate key in a primary key or unique index fails, including two rows of the same VALUES list.
  • Referential integrity: a row whose foreign key has no matching row in the parent table fails.
  • A table-level CHECK constraint is enforced.

The statement may end with WITH OWNERACCESS OPTION, as ACE allows. LibRed has no users, so the option changes nothing.

Differences from ACE

  • LibRed accepts a VALUES list of several rows and a set operation as the source (above). ACE does not.
  • ACE refuses an IN external database unless its AllowQueryRemoteTables setting allows it. LibRed has no such setting.

These run against the Northwind sample database, as in the playground. They follow on from each other.

Create a table to insert into:

CREATE TABLE Notes (Id COUNTER PRIMARY KEY, Topic TEXT(50) DEFAULT 'General',
Body TEXT(255), Created DATETIME DEFAULT Now())

Insert one row. Id and Created are generated:

INSERT INTO Notes (Topic, Body) VALUES ('Orders', 'Check the late orders')

Insert three rows at once. The second takes Topic’s default, 'General'; the third stores Null in Body:

INSERT INTO Notes (Topic, Body)
VALUES ('Shipping', 'Call Speedy Express'), (DEFAULT, 'No topic given'), ('Stock', NULL)

Insert a row of defaults only:

INSERT INTO Notes DEFAULT VALUES

Append rows from a query:

INSERT INTO Notes (Topic, Body)
SELECT 'Customer', CompanyName FROM Customers WHERE Country = 'Norway'

Append from a union:

INSERT INTO Notes (Topic, Body)
SELECT 'Supplier', CompanyName FROM Suppliers WHERE Country = 'Norway'
UNION ALL
SELECT 'Shipper', CompanyName FROM Shippers WHERE ShipperID = 1

Append without a column list. The aliases choose the columns:

INSERT INTO Notes
SELECT 'Category' AS Topic, CategoryName AS Body FROM Categories WHERE CategoryID = 1

This fails, because Northwind has no customer ZZZZZ and Orders has a relationship to Customers:

INSERT INTO Orders (CustomerID, EmployeeID, OrderDate) VALUES ('ZZZZZ', 1, #2026-01-15#)

Copy the shippers into a table in another database. This assumes C:\Data\Archive.accdb exists and has a table ShipperArchive (Id COUNTER, CompanyName TEXT(40), Phone TEXT(24)):

INSERT INTO ShipperArchive (CompanyName, Phone) IN 'C:\Data\Archive.accdb'
SELECT CompanyName, Phone FROM Shippers