SQL statement import/export Excel (reproduced from: White so blog)

Source: Internet
Author: User

 

Import / Export and generate EXCEL
-- To import data from an Excel file to a SQL database, you can simply use the following statement:
/**/ /**/ /**/ /*========================================================== ======================================*/
-- If the table to be imported already exists
Insert   Into Table Select   *   From  
OpenRowSet ( ' Microsoft. Jet. oledb.4.0 '
, ' Excel 5.0; HDR = yes; database = c: \ test.xls ' , Sheet1 $)

-- If you import data and generate a table
Select   *   Into Table From  
OpenRowSet ( ' Microsoft. Jet. oledb.4.0 '
, ' Excel 5.0; HDR = yes; database = c: \ test.xls ' , Sheet1 $)


/**/ /**/ /**/ /*========================================================== ======================================*/
-- If you export data from the SQL database to excel, if the Excel file already exists and you have created a header based on the data to be received, you can simply use it:
Insert   Into   OpenRowSet ( ' Microsoft. Jet. oledb.4.0 '
, ' Excel 5.0; HDR = yes; database = c: \ test.xls ' , Sheet1 $)
Select   *   From Table


-- If the Excel file does not exist, you can use bcp to convert it into an Excel-like file. Note the case sensitivity:
-- Export tables
Exec Master .. xp_mongoshell ' BCP database name. DBO. Table name out "C: \ test.xls"-C-s "server name"-U "username"-P "password" '

-- Export Query Information
Exec Master .. xp_mongoshell ' BCP "select au_fname, au_lname from pubs .. authors order by au_lname" queryout "C: \ test.xls"-C-s "server name"-U "username"-P "password" '

/**/ /**/ /**/ /*-- Note:
C: \ test.xls is the Excel file name for import/export.
Sheet1 $ is the worksheet name of the Excel file. You must add $ to use it normally.
--*/

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.