Get Excel sheet into temp table using a script

前端 未结 3 1357
北恋
北恋 2020-12-10 14:13

Im trying to get this excel sheet into a table, so I can apply select statements to it etc, to update tables with its info.

SELECT * 
FROM OPENROWSET(\'Micro         


        
3条回答
  •  半阙折子戏
    2020-12-10 15:14

    There are 5 possible causes for this error.

    1. The jet engine must be installed on the server. Installing MS Office on the server sorts that out.
    2. The path to the xls is relative to the server, not the workstation you're running the command from
    3. The account that runs the SQL server service must have write access to the folder where the xls is. One possible solution is to changing the temp= and tmp= environment variables of the service startup account (eg. Administrator) to (for example) c:\temp, then enable Full Control on c:\temp to Everyone

    4...

    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ad Hoc Distributed Queries', 1;
    GO
    RECONFIGURE;
    GO
    

    5....

    EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'AllowInProcess', 0 
    GO 
    EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'DynamicParameters', 0
    GO 
    

    Now I don't know why this works, especially considering that everyone else says they should be set to 1. For me however, setting them to zero, did the trick for the following SQL statement on SQL Server 2008R2 32bit and M$ Office 2007

    Select * 
    into [temp_table$]
    FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;Database=C:\temp\EXPENDITURE REPORT.xls;HDR=YES;IMEX=1',
    'SELECT * FROM [EXPENDITURE SHEET$]')
    

    Note: I purposely have used an example in which both the filename and the worksheet name have spaces to show that this can be done.

提交回复
热议问题