34,590
社区成员
发帖
与我相关
我的任务
分享
-- =============================================
-- Author: xujing
-- Create date: 2009-04-02
-- Description: read datasource(excel ,xml ,access...)
-- =============================================
ALTER PROCEDURE [dbo].[UP_ReadDataSource]
@filename nvarchar(500)
AS
BEGIN
--現已經建議X盤的遠程文件映射,故注釋部分不再需要
exec sp_configure 'show advanced options',1
reconfigure WITH OVERRIDE
--如系統未建立映射,則應開啟 xp_cmdshell 組件,使之能建立映射
exec sp_configure 'xp_cmdshell',1
reconfigure WITH OVERRIDE
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure WITH OVERRIDE
--如系統未建立映射,則在開啟 xp_cmdshell 組件后,使用下面語句建立遠程文件的映射
exec master..xp_cmdshell 'net use k: \\10.202.1.54\kaikai_wo linmoug123 /user:10.202.1.54\administrator'
declare @sqlstr nvarchar(1000)
set @sqlstr='SELECT * FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;HDR=YES;IMEX=1;DataBase=k:\TrolleyManagement\Excel\'+@filename+'.xls'',Sheet1$);';
exec(@sqlstr);
--刪除映射
--exec master..xp_cmdshell 'net use k: /delete'
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure WITH OVERRIDE
--如系統未建立映射,則應開啟 xp_cmdshell 組件并使用結束后后,再關閉該組件
exec sp_configure 'xp_cmdshell',0
reconfigure WITH OVERRIDE
exec sp_configure 'show advanced options',0
reconfigure WITH OVERRIDE
END
組態選項 'show advanced options' 從 1 變更為 1。請執行 RECONFIGURE 陳述式來安裝。
組態選項 'Ad Hoc Distributed Queries' 從 1 變更為 1。請執行 RECONFIGURE 陳述式來安裝。
連結伺服器 "(null)" 的 OLE DB 提供者 "Microsoft.Jet.OLEDB.4.0" 傳回訊息 "無法指出的錯誤"。
消息 7303,级别 16,状态 1,第 1 行
無法初始化連結伺服器 "(null)" 的 OLE DB 提供者 "Microsoft.Jet.OLEDB.4.0" 的資料來源物件。
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
--excel語法
select a,b,c
from openrowset('Microsoft.Jet.OLEDB.4.0','EXCEL 8.0;HDR=YES;User id=admin;Password=;IMEX=1;
DATABASE=C:\temp.xls', temp$)
SELECT * FROM OpenDataSource
( 'Microsoft.Jet.OLEDB.4.0','Data Source="C:\Item.xls";User ID=admin;Password=;Extended properties="Excel 8.0;HDR=Yes;";Persist Security Info=False')...[Items$]
--Access語法
SELECT CustomerID, CompanyName FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb'; 'admin';'',Customers)