Use OpenRowSet to import data from Excel to SQL Server

Source: Internet
Author: User
Tags sql server query how to use sql
During project implementation, users often need to provide an Excel template, allow users to enter a basic data (such as department staff data, online exam question library, and device information) in EXCEL according to their actual conditions ), one reason is that you are familiar with Excel. The other reason is that you can directly import data from other data sources instead of entering data from the beginning.

After the user handed over the processed workbook to us, the general implementers will use the tools they are familiar with to import the Excel file into the system data table. I have been using tools to import it all the time, but today, a colleague told me that I could directly use the SQL Server Query analyzer to complete this function. I have never tried it before. So I tried it and found it really good and I don't need a tool, no need to configure, just a simple SQL statement. Next let's take a look at how to use SQL statements to import EXCEL to SQL Server. Step 1: place the prepared Excel file on the local machine. Note that the xls file name is in English, and the first line of XLS should be the field name. Step 2: Open the query analyzer and enter the following statement: -- enable ad hoc distributed queriesexec sp_configure 'show advanced options', 1 reconfigure exec sp_configure 'ad hoc distributed queries ', 1 reconfigure insert into rt_para (xlbh, msid, ywgd, DHZ) -- database table name, field name select xlbh, msid, ywgd, DHZ -- field name in Excel from OpenRowSet ('Microsoft. jet. oledb.4.0 ', 'excel 5.0; HDR = yes; database = D: \ yewei.xls', sheet1 $) -- note that XLS should be written to the physical location -- disable ad hoc distributed queriesexec sp_configure 'ad hoc distributed queries ', 0
Reconfigure
Exec sp_configure 'show advanced options', 0
Reconfigure Step 3: Execute the statement to complete the import operation. How are you doing?
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.