TL;DR – You can use SQL SELECT INTO to duplicate information from one table to another. It can also produce a unique table which is then placed in a file group. The new table cannot be defined as a table variable.
Contents
- 1. The syntax for SQL SELECT INTO
- 1.1. Copying all columns to a table
- 1.2. Copying particular columns to a table
- 2. SQL SELECT INTO usage examples
- 2.1. Backing up a table
- 2.2. Copying a table to other database
- 2.3. Only copying a few columns
- 2.4. Only copying entries from a single country
- 2.5. Copying multiple tables into a new one
The syntax for SQL SELECT INTO
Copying all columns to a table
Example
SELECT *
INTO ntable [IN edb]
FROM otable
WHERE condition;
Note: The names/types are described in SELECT. New names can be assigned using the AS clause.
Copying particular columns to a table
Example
SELECT column1, column2, column3, ...
INTO ntable [IN edb]
FROM otable
WHERE condition;
SQL SELECT INTO usage examples
Backing up a table
Example
SELECT * INTO CustomerBackup2018
FROM Customers;
Copying a table to other database
Example
SELECT Name, Contact INTO CustomerBackup2017
FROM Customers;
Only copying a few columns
Example
SELECT * INTO CustomerBackup2017 IN 'Backup.mdb'
FROM Customers;
Only copying entries from a single country
Example
SELECT * INTO CustomerGermany
FROM Customers
WHERE Country = 'Germany';
Copying multiple tables into a new one
Example
SELECT Customers.Name, Customer_orders.ID
INTO CustomerOrderBackup2017
FROM Customers
LEFT JOIN Order ON Customers.ID = Customer_orders.Customer_id;