2

一项作业,其中 proc 正在使用 openrowset 函数读取 excel 文件,然后使用 bcp 命令导出文件。直到最近它工作得很好。当我运行工作时,它给了我这个错误: -

NT 当局\本地服务。无法为链接服务器“(null)”初始化 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”的数据源对象。[SQLSTATE 42000](错误 7303)配置选项“显示高级选项”从 1 更改为 1。运行 RECONFIGURE 语句进行安装。[SQLSTATE 01000](错误 15457)配置选项“Ad Hoc Distributed Queries”从 1 更改为 1。运行 RECONFIGURE 语句进行安装。[SQLSTATE 01000](错误 15457)链接服务器“(null)”的 OLE DB 提供程序“Microsoft.Jet.OLEDB.4.0”返回消息“未指定的错误”。[SQLSTATE 01000](错误 7412)。步骤失败。

这是存储过程代码:-

ALTER PROCEDURE [dbo].[Read_Excel]
     @ExcelFilePath varchar(500)
    ,@OutPutFilePath nvarchar(500)
    ,@ServerName nvarchar(500)
    ,@DatabaseName nvarchar(100)
    ,@UserName nvarchar(50)
    ,@Password nvarchar(50)
    ,@Delimiter char(1)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @Query nvarchar(1000)
    DECLARE @Cmd varchar(1000)
    DECLARE @FileExists int

    -- Check File Existence
    EXEC master..xp_fileexist @ExcelFilePath, @FileExists OUTPUT --returns 1 if exists, 0 if file is not there
    if @FileExists <> 1
        BEGIN PRINT 'There is no excel file available: ' + @ExcelFilePath RETURN END

    -- Allow Ad hoc Distributed Queries in order to run OpenRowset Function
    EXEC SP_CONFIGURE 'show advanced options', 1
    RECONFIGURE
    EXEC SP_CONFIGURE 'Ad Hoc Distributed Queries', 1
    RECONFIGURE

    -- Clear tbl_excel Table
    TRUNCATE TABLE tbl_Excel
    --Read EXCEL File using OPENROWSET Function 
    SET @Cmd = 'INSERT INTO tbl_Excel
                SELECT * FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'', ''Excel 8.0;HDR=YES;IMEX=1;Database=' + @ExcelFilePath + ''',
                ''SELECT * FROM [Sheet1$]'')'
    EXEC(@Cmd)
    -- Allow Ad hoc Distributed Queries in order to run OpenRowset Function
    EXEC SP_CONFIGURE 'show advanced options', 1
    RECONFIGURE
    EXEC SP_CONFIGURE 'Ad Hoc Distributed Queries', 0
    RECONFIGURE

    --Query View
    SET @Query = 'SELECT id1, name1, name2, address1, address2 FROM [' + @DatabaseName + '].[dbo].[tbl_Excel]'
    SET @Cmd = 'bcp "' + @Query + '" queryout "' +  @OutPutFilePath +
                 '" -c -S' + @ServerName + ' -U' + @UserName + ' -P' + 
                @Password + ' -t' + @Delimiter + ''
    EXEC master..xp_cmdshell @Cmd
END

提前致谢。

4

1 回答 1

0

您是否尝试过更改 SQL Server 代理登录?(服务-> SQL Server 代理,右键单击登录)到您的用户帐户?或尝试不使用 JET 而使用 ACE

SET @Cmd = 'INSERT INTO tbl_Excel
    SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 8.0;HDR=YES;IMEX=1; 'Database=' + @ExcelFilePath + ', 'SELECT * FROM [Sheet1$]')'
于 2013-04-25T13:56:59.753 回答