Import Excel to SQL Server 2008

前端 未结 4 953
广开言路
广开言路 2021-01-04 20:41

I need to create a process to import a multi tabbed excel spreadsheet into SQL Server 2008R2. Each tab will be a different table in the database. This will need to be done w

相关标签:
4条回答
  • 2021-01-04 20:55

    Below is the code to insert data from a csv file into a given table. I don't what the full requirements are for the project, but if I were you I would just separate each table into a different file and then just run a proc that inserts data into each of the tables.

    BULK
    INSERT TABLE_NAME
    FROM 'c:\filename.csv'
    WITH
    (
      FIELDTERMINATOR = ',',
      ROWTERMINATOR = '\n'
    )
    
    insert into import_history ('filename', 'import_date') values ('your_file_name', getdate())
    

    Also, for the table that tracks imports and timestamps them, you could just insert some data into that table after each bulk insert as seen above.

    Also, here's a link to tutorial on bulk inserting from a csv file that may also help: http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/

    0 讨论(0)
  • 2021-01-04 21:01

    If you're limited solely to TSQL, the above two answers will show you some ideas. If you have access to either Data Tools or Business Intelligence, with SSIS, you can automate it with the assumption that each sheet in the Excel workbook matches each time. With SSIS, you'll use a Data Flow task and each sheet will be imported into the table that you want. When you're ready for the file the next week, you'll drop it into the folder and run the SSIS package.

    However, if the sheet names change, (for instance, one week sheets are called Cats, Dogs, Rain and the next week it's Sulfur, Fire, Hell) then this would cause the package to break. Otherwise, if only the data within the worksheet change, then this can be completely automated with SSIS.

    Example article: https://www.simple-talk.com/sql/ssis/moving-data-from-excel-to-sql-server---10-steps-to-follow/

    0 讨论(0)
  • 2021-01-04 21:02

    There is a nice article by microsoft - http://support.microsoft.com/kb/321686 - that outlines the processes involved.

    The process is simply

    SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
       'Data Source=C:\test\xltest.xls;Extended Properties=Excel 8.0')...[Customers$]
    

    Where XLImport3 is the table you want to import into and the datasource is the excel sheet you want to import from.

    0 讨论(0)
  • 2021-01-04 21:08

    Its very simple. Right click the Database in Sql Server(2008), select Tasks and select Import Data

    enter image description here



    Now change the DataSource to Microsoft Excel. Chose the path of Excel file by clicking Browse button and click Next.

    enter image description here



    Chose the Sql Server instance and chose the database to which the excel to be imported.

    enter image description here



    Select Copy data from one or more tables or views and click Next.

    enter image description here



    Now select the sheets to be imported to Sql Server.

    enter image description here



    Click Next

    enter image description here



    Now click Finish

    enter image description here



    Now the wizard imports the data from Excel to Sql Server and click Close.

    enter image description here



    Here is the table

    enter image description here

    0 讨论(0)
提交回复
热议问题