2011-10-18 95 views
2

作業,其中proc使用openrowset函數讀取excel文件,然後使用bcp命令導出文件。直到最近它一直在完美地工作。當我運行工作它給我這個錯誤: -如何解決我在運行SQl Sever代理作業時遇到的此錯誤?

NT AUTHORITY\LOCAL SERVICE. Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)". [SQLSTATE 42000] (Error 7303) Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install. [SQLSTATE 01000] (Error 15457) Configuration option 'Ad Hoc Distributed Queries' changed from 1 to 1. Run the RECONFIGURE statement to install. [SQLSTATE 01000] (Error 15457) OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "Unspecified error". [SQLSTATE 01000] (Error 7412). The step failed.

這裏是存儲過程的代碼: -

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 

在此先感謝。

+0

我看來像你要麼動態填充的鏈接服務器名稱,第二它沒有被傳遞給PROC - 或者,鏈接服務器已停止鏈接? – Widor

+0

Widor,檢查proc,如果這給出了任何線索 – User13839404

+3

霍利廢話。我只是從本地服務將SQL Server代理服務帳戶更改爲NetworkService。它的工作。 – User13839404

回答

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$]')' 
相關問題