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.
Syntax
Section titled “Syntax”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 ] [ , … ] ]Arguments
Section titled “Arguments”select_item
Section titled “select_item”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.
new_table
Section titled “new_table”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.
IN external_database (after INTO)
Section titled “IN external_database (after INTO)”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.
FROM … [ IN external_database ]
Section titled “FROM … [ IN external_database ]”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.
WHERE, GROUP BY, HAVING, ORDER BY, TOP
Section titled “WHERE, GROUP BY, HAVING, ORDER BY, TOP”As in SELECT. The new table holds whatever rows the query returns, aggregates included.
Remarks
Section titled “Remarks”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.
Differences from ACE
Section titled “Differences from ACE”ROUNDof a Decimal column makes a Decimal column. ACE makes it a Double. LibRed’sRoundkeeps its operand’s type, so the new table holds what theSELECTreturns.TOP nreturns exactly n rows, here as inSELECT, where ACE’sTOP nalso returns the rows tied with the last one.INexternal_database has noAllowQueryRemoteTablesgate. ACE refuses it unless that setting allows it.
Examples
Section titled “Examples”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.CompanyNameINTO OrderArchiveFROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerIDWHERE Orders.OrderDate < #1997-01-01#A computed column, named by its alias:
SELECT ProductID, UnitPrice * UnitsInStock AS StockValue INTO StockValues FROM ProductsA summary table, one row per category:
SELECT CategoryID, COUNT(*) AS Products, AVG(UnitPrice) AS AvgPrice INTO CategorySummaryFROM Products GROUP BY CategoryIDAn empty copy of a table’s columns:
SELECT * INTO EmptyShippers FROM Shippers WHERE FalseThe copy has no primary key, so add one:
SELECT * INTO ProductsCopy FROM ProductsCREATE INDEX pkProductsCopy ON ProductsCopy (ProductID) WITH PRIMARYMake 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 ShippersMake a table here from another database’s rows:
SELECT * INTO Shippers2 FROM Shippers IN 'C:\Data\Archive.accdb'