使用“openrowset”的 sql 脚本出错

发布于 2024-10-07 11:24:08 字数 724 浏览 0 评论 0原文

我的 SQL 脚本有问题:

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

当我尝试执行此脚本时,它会生成以下错误:

Msg 7357,Level 16,State 2,Line 1 无法处理对象“SELECT 手机号码, 姓, 名 FROM [SampleData$]”。链接服务器“(null)”的 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”表示该对象没有列,或者当前用户对该对象没有权限。

是否有有什么解决方案吗?在过去的3个小时里我真的找不到任何解决方案..基本上,我只想操作excel文件中的数据,然后将其保存到sql server 2005数据库,但现在,我想检索数据从excel文件到sql服务器..感谢您的帮助..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2024-10-14 11:24:08

我让它与本地电子表格一起使用。忘记OPENROWSET

  1. 在 Excel 电子表格中创建命名范围。为此,突出显示所需的列(包括标题),右键单击并选择“命名范围”。为其命名,这将是您的表名称。

    http://www.homeandlearn.co.uk/me/mes9p2.html< /a>

  2. 保存并关闭您的电子表格。如果您打开它,SQL Server 将无法访问它。

  3. 添加链接服务器。请按照下面E 部分中的说明进行操作,该说明告诉您如何为 Excel 电子表格添加链接服务器:

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

  4. 您应该能够非常愉快地查询 DS,再次按照指示。

这是适合我的代码:

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

SELECT *
   FROM ExcelSource4...MyTable2;

最后。开始接受一些答案并对任何有帮助的答案进行投票。这是 StackOverflow 的命脉。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文