The document discusses using the INSERT INTO statement with a SELECT statement to copy data from one table to another. It provides an example of creating a new table and then using INSERT INTO with SELECT to copy over all records from the original table to the new table. The columns in the destination and source tables must match in number and data type for the query to work.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
23 views
Using INSERT INTO With The SELECT Statement
The document discusses using the INSERT INTO statement with a SELECT statement to copy data from one table to another. It provides an example of creating a new table and then using INSERT INTO with SELECT to copy over all records from the original table to the new table. The columns in the destination and source tables must match in number and data type for the query to work.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
Using INSERT INTO with
the SELECT Statement
This final section details how a SELECT statement can supply the data to be inserted into a table using the INSERT INTO statement. Using these two statements in conjunction with one another is useful for copying data from one table to another. For example, if you decide to restructure a databases table design but need to retain all the data, you would use the combination of INSERT INTO and SELECT statements. You could create your new tables and then use INSERT INTO with SELECT to copy the data over. The combination is also useful if you want to create a new identical copy of a databasefor example, for backup purposes or if you need to move the database to a new computer. The basic syntax is as follows: INSERT INTO destination_table_name SELECT column_list FROM source_table_name WHERE condition If you use this syntax, the destination columns and source columns must match. The number of columns and appropriate data types must match, although theres no need for column names to match. The WHERE clause is optional and is needed only if you dont want to copy over all records. For example, if you want to create a copy of the Location table, first you need to create the table: CREATE TABLE AnotherLocation ( LocationId int, Street varchar(50), City varchar(50), State varchar(50) ); After creating the table, you can copy over the data: INSERT INTO AnotherLocation SELECT LocationId, Street, City, State FROM Location WHERE State IN (New State ,Golden State);