Thursday, 28 November 2013

SELECT INTO Statement



You can copy information from one table into another. 
 The SELECT INTO statement copies data from one table and inserts it into a new table.


SELECT *

INTO newtable [IN externaldb]
 
FROM table1;

- selects data from one table and inserts it into a new table. 


SELECT column_name(s)
 
INTO newtable [IN externaldb]
 
FROM table1; 

- copy only the columns we want into the new table. 




Qs:  Create a backup copy of Customers:

SELECT *
INTO CustomersBackup2013
FROM Customers; 

Qs: Use the IN clause to copy the table into another database: 

SELECT *
INTO CustomersBackup2013 IN 'Backup.mdb'
FROM Customers;

Qs: Copy only a few columns into the new table:

SELECT CustomerName, ContactName
INTO CustomersBackup2013
FROM Customers; 


Qs: Copy only the German customers into the new table:

SELECT *
INTO CustomersBackup2013
FROM Customers
WHERE Country='Germany';

Qs: Copy data from more than one table into the new table:

SELECT Customers.CustomerName, Orders.OrderID
INTO CustomersOrderBackup2013
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;

 


 
 

No comments:

Post a Comment