Search the Whole World Here.,.,

SQL - INSERT INTO SELECT Examples




Copy only a few columns from "Suppliers" into "Customers":

Example

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers;
Try it Yourself »
Copy only the German suppliers into "Customers":

Example

INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='India';
Try it Yourself »

SQL - INSERT INTO SELECT Syntax




We can copy all columns from one table to another, existing table:

INSERT INTO table2
SELECT * FROM table1;
Or we can copy only the columns we want to into another, existing table:

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

SQL - SELECT INTO Examples




Create a backup copy of Customers:

SELECT *
INTO CustomersBackup2017
FROM Customers;
Use the IN clause to copy the table into another database:

SELECT *
INTO CustomersBackup2017 IN 'Backup.mdb'
FROM Customers;
Copy only a few columns into the new table:

SELECT CustomerName, ContactName
INTO CustomersBackup2017
FROM Customers;
Copy only the German customers into the new table:

SELECT *
INTO CustomersBackup2017
FROM Customers
WHERE Country='India';
Copy data from more than one table into the new table:

SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2017
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
Tip: The SELECT INTO statement can also be used to create a new, empty table using the schema of another. Just add a WHERE clause that causes the query to return no data:

SELECT *
INTO newtable
FROM table1
WHERE 1=0;

Ads by Google