2011-10-18 2 views
2

Un travail dans lequel un proc lit le fichier Excel à l'aide de la fonction openrowset, puis exporte un fichier à l'aide de la commande bcp. Cela fonctionnait parfaitement jusqu'à récemment. Quand je lance ce travail me donne cette erreur: -Comment résoudre cette erreur lors de l'exécution du job d'agent 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.

ici est stocké code de procédure: -

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 

Merci à l'avance.

+0

Attend à moi comme vous soit peuplez dynamiquement le nom de serveur lié, nd il a échoué à passer au Proc - ou, le serveur lié a cessé d'être lié ? – Widor

+0

Widor, vérifiez le proc, si cela donne une idée – User13839404

+3

Holly merde. Je viens de changer le compte de service de l'Agent SQL Server en NetworkService de localservice. Et ça a marché. – User13839404

Répondre

0

Avez-vous essayé de modifier la connexion de l'Agent SQL Server? (Services-> Agent SQL Server, cliquez avec le bouton droit sur Connexion) à votre compte d'utilisateur? ou essayer de ne pas utiliser JET utiliser à la place 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$]')' 
Questions connexes