error on sql script with 'openrowset'

懵懂的女人 提交于 2019-12-10 10:29:58

问题


I'm having a problem with my SQL script:

SELECT  
  SP.[MobileNumber],  
  SP.[LastName],  
  SP.[FirstName]  
FROM SampleTable1 SP  
INNER JOIN OPENROWSET  
(  
  'Microsoft.Jet.OLEDB.4.0',  
  'Excel 8.0;Database=C:\devpc11\sample.xls;',  
  'SELECT   
     MobileNumber,   
     LastName,  
     FirstName  
   FROM [SampleData$]') T  
ON SP.[MobileNumber] = T.[MobileNumber]  
GO

when i try to execute this, it generates this error:

Msg 7357, Level 16, State 2, Line 1 Cannot process the object "SELECT MobileNumber, LastName, FirstName FROM [SampleData$]". The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" indicates that either the object has no columns or the current user does not have permissions on that object.

Is there any solution for this? i really can't find any in the past 3 hours.. Basically, i just want to manipulate data from an excel file, then save it to sql server 2005 database, but for now, i want to retrieve data from the excel file to the sql server.. thanks for the help..


回答1:


I got this to work with a spreadsheet locally. forget OPENROWSET

  1. Create a named range in your excel spreadheet. Tio do this, highlight the columns (including headers) you want, right-click and select 'Name a range'. Give this a name, this will be your table name.

    http://www.homeandlearn.co.uk/me/mes9p2.html

  2. Save and close your spreadsheet. SQL Server wont be able to access it if you hve it open.

  3. Add a linked server. Follow the instructions in Section E in the following which tells you how to add a linked server for Excel Spreadsheets:

    http://msdn.microsoft.com/en-us/library/ms190479.aspx

  4. You should be able to query the DS quite happily, again following the instructions.

Here is the code that works for me:

EXEC sp_addlinkedserver 'ExcelSource4',
   'Jet 4.0',
   'Microsoft.Jet.OLEDB.4.0',
   'c:\sqlss.xls',
   NULL,
   'Excel 5.0';
GO

SELECT *
   FROM ExcelSource4...MyTable2;

And finally. Start accepting some answers and voting up any helpful ones. This is the lifeblood of StackOverflow.



来源:https://stackoverflow.com/questions/4437172/error-on-sql-script-with-openrowset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!