简体   繁体   中英

Stored Procedure results in Excel

I have a requirement to provide the results of my analysis in different sheets under one excel file. Currently i'm manually copying and pasting the results into excel (10 sheets). How I can use the stored procedure to automate this?

I have tried something like below.

insert into OPENROWSET(
   'Microsoft.Jet.OLEDB.4.0', 
   'Excel 8.0;Database=D:\Results\test.xls;;HDR=YES', 
   'SELECT * FROM [Sheet1$]')
select * from MY_TABLE WHERE ATTR='YES'

insert into OPENROWSET(
   'Microsoft.Jet.OLEDB.4.0', 
   'Excel 8.0;Database=D:\Results\test.xls;;HDR=YES', 
   'SELECT * FROM [Sheet2$]')
select * from MY_TABLE WHERE ATTR='No'

It is giving me error

SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries'

I will try to get the access from admin.Hopefully after that it will work? But Is there any other way of doing this? Also I want to know how to do this inside a procedure for providing the results in different sheets.

Try this code:

EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'AllowInProcess', 1
GO 
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.Jet.OLEDB.4.0', N'DynamicParameters', 1
GO 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM