Skip to content

SELECT … INTO

Creates a new table and fills it with the rows of a query. Access calls this a make-table query.

Access SQL. ACE accepts this statement as written here. The query part takes everything SELECT does, LibRed’s extensions included.

SELECT [ ALL | DISTINCT | DISTINCTROW ] [ TOP n [ PERCENT ] ]
select_item [ , … ]
INTO new_table [ IN external_database ]
[ FROM table_source [ , … ] [ IN external_database ] ]
[ WHERE condition ]
[ GROUP BY expression [ , … ] ]
[ HAVING condition ]
[ ORDER BY expression [ ASC | DESC ] [ , … ] ]

A column of the new table. A column copied from a source column takes that column’s name and type. An expression takes its alias as its name, and its type from the expression: Qty * 2 over a Long Integer column is a Long Integer, Label & '!' is Text. A computed Decimal is a Decimal of the result’s precision and scale.

* copies every column of the source.

The name of the table to create. The statement fails with “Table ‘new_table’ already exists.” if a table or a query already has that name: tables and queries share one set of names.

Creates the new table in another Access database instead of this one. It takes the same forms as after FROM:

IN 'C:\Data\Archive.accdb'
IN 'C:\Data\Archive.accdb' 'MS Access;'
IN 'C:\Data\Archive.accdb' [MS Access;]
IN '' [;DATABASE=C:\Data\Archive.accdb]

The name must be free in that database. Only Access databases are opened: a dBASE, Paradox, Excel or text type is refused.

The rows to copy, as in SELECT. An IN after the FROM clause reads the source tables from another database. The two IN clauses are independent: a statement can read from one file and write to another, or read and write the same other file.

As in SELECT. The new table holds whatever rows the query returns, aggregates included.

The new table gets the columns and the rows, and nothing else. The source’s primary key and indexes are not copied, so a copy of a keyed table comes back unkeyed. Add a key or an index afterwards with CREATE INDEX or ALTER TABLE.

A query that returns no rows still creates the table, empty.

The statement returns no rows. Its result is the number of rows it copied, as ExecuteNonQuery reports it.

A make-table query can be stored with CREATE PROCEDURE and run with EXECUTE, its IN clause included.

  • ROUND of a Decimal column makes a Decimal column. ACE makes it a Double. LibRed’s Round keeps its operand’s type, so the new table holds what the SELECT returns.
  • TOP n returns exactly n rows, here as in SELECT, where ACE’s TOP n also returns the rows tied with the last one.
  • IN external_database has no AllowQueryRemoteTables gate. ACE refuses it unless that setting allows it.

These run against the Northwind sample database, as in the playground. Each creates its own table.

Copy the German customers into a new table:

SELECT CustomerID, CompanyName INTO GermanCustomers FROM Customers WHERE Country = 'Germany'

Copy a join, with a date filter:

SELECT Orders.OrderID, Orders.OrderDate, Customers.CompanyName
INTO OrderArchive
FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
WHERE Orders.OrderDate < #1997-01-01#

A computed column, named by its alias:

SELECT ProductID, UnitPrice * UnitsInStock AS StockValue INTO StockValues FROM Products

A summary table, one row per category:

SELECT CategoryID, COUNT(*) AS Products, AVG(UnitPrice) AS AvgPrice INTO CategorySummary
FROM Products GROUP BY CategoryID

An empty copy of a table’s columns:

SELECT * INTO EmptyShippers FROM Shippers WHERE False

The copy has no primary key, so add one:

SELECT * INTO ProductsCopy FROM Products
CREATE INDEX pkProductsCopy ON ProductsCopy (ProductID) WITH PRIMARY

Make the table in another database. C:\Data\Archive.accdb stands for another Access database; put the path of your own file in its place:

SELECT * INTO ShippersCopy IN 'C:\Data\Archive.accdb' FROM Shippers

Make a table here from another database’s rows:

SELECT * INTO Shippers2 FROM Shippers IN 'C:\Data\Archive.accdb'