Importing data from one table to another in mysql

Source: Internet
Author: User


In mysql, data from one table is imported into another table in batches, whether in website development or application development, we often encounter the need to batch import data from a table in MySQL or MS SQLServer to another table, and sometimes even specify the import field. Www.2cto.com this article takes the MySQL database as an example to introduce how to import all data of a table or data of specified fields to the target table through SQL command lines. This method also applies to SQLServer databases, that is, T-SQL. Category 1: If the fields of the two tables (the export table and the target table) are the same and you want to insert all the data, you can use this method: (this method is only suitable for exporting the two tables in the same database) insert into target table SELECT * FROM source table. For example, to INSERT the articles table INTO the newArticles table, you can use the following SQL statement: INSERT INTO newArticles SELECT * FROM articles; type 2: If you only want to import specified fields, you can use this method: www.2cto.com insert into target table (Field 1, Field 2 ,...) SELECT Field 1, Field 2 ,... FROM source table; note that the fields in the above two tables must be consistent (field type); otherwise, a Data Conversion error occurs. 1. insert into openrowset ('sqlodb', 'localhost'; 'sa '; '123', Test. dbo. table_ B) select * from Test. dbo. table_A // enable Ad Hoc Distributed Queries: exec sp_configure 'show advanced options', 1 reconfigureexec sp_configure 'ad Hoc Distributed Queries ', 1 reconfigure www.2cto.com // after use, disable Ad Hoc Distributed Queries: exec sp_configure 'ad Hoc Distributed Queries ', 0 reconfigureexec sp_configure 'show advanced options', 0 reconfigure
2. // The server insert into dbo. table_ B) select * from dbo. table_A Concatenates the table name and database connection string with code and then executes the SQL statement program function you need to complete

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.