开发者

Importing the resultset of a query from SQL Server 2005 into MS Excel

开发者 https://www.devze.com 2023-01-28 22:00 出处:网络
I tried executing this stored procedure that is meant to copy the format of an already created excel sheet into another excel sheet; the former serves as a template. The stored procedure is then meant

I tried executing this stored procedure that is meant to copy the format of an already created excel sheet into another excel sheet; the former serves as a template. The stored procedure is then meant to populate the new excel sheet with the resultset from a SQL query.

When executing, it gives the following error:

    Insert ExcelSource...[ExcelTable$]  ( A,B,C ) select convert(varchar(200),USER_ID), F开发者_StackOverflow社区IRST_NAME, 
Convert (varchar(20),CREATEDTIME) 
from SERV..AaUser
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource" returned message "Cannot start your application. The workgroup information file is missing or opened exclusively by another user.".
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource" reported an error. Authentication failed.
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ExcelSource".

the syntax for the stored procedure is:

    Create proc sp_write2Excel (@fileName varchar(100),

                                   @NumOfColumns tinyint,

                                   @query     varchar(200))

as

begin

        declare @dosStmt  varchar(200) 

        declare @tsqlStmt varchar(500) 

        declare @colList  varchar(200) 

        declare @charInd  tinyint 



        set nocount on



        -- construct the  columnList A,B,C ... 

        -- until Num Of columns is reached.



        set @charInd=0

        set @colList = 'A'

        while @charInd < @NumOfColumns - 1

        begin 

          set @charInd = @charInd + 1

          set @colList = @colList + ',' + char(65 + @charInd)

        end 



        -- Create an Empty Excel file as the target file name by copying the template Empty excel File

        set @dosStmt = ' copy C:\emp\empty.xls ' + @fileName

        exec master..xp_cmdshell @dosStmt



        -- Create a "temporary" linked server to that file in order to "Export" Data

        EXEC sp_addlinkedserver 'ExcelSource', 

        'Jet 4.0',

        'Microsoft.Jet.OLEDB.4.0',

        @fileName,

        NULL,

        'Excel 5.0'



        -- construct a T-SQL statement that will actually export the query results

        -- to the Table in the target linked server 

        set @tsqlStmt = 'Insert ExcelSource...[ExcelTable$] ' +  ' ( ' + @colList + ' ) '+ @query



        print @tsqlStmt



        -- execute dynamically the TSQL statement

        exec (@tsqlStmt)



        -- drop the linked server 

        EXEC sp_dropserver 'ExcelSource' 

        set nocount off

end 

Many thanks for your audience and anticipated help.

Cheers, Tunde


Is Excel installed on the same machine as your SQL Server instance? There's a possibility that Office of the JET driver is missing.

Edit:

I think I misread the post - it sounds like the file is open already. Excel files can only be opened by one user at a time, and SQL Server is requiring exclusive access to this file. Using LockHunter may help determine what's tying the file up.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号