I did an intensive search and could not seem to find an answer.
I want to export SQL queries to Excel and return it (the excel) to the client application or to my web-service. (think about a similar concept like FOR XML
but instead return binary Excel data)
I tested this (after a long struggle) and it finally works fine:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\1.xls;',
'SELECT * FROM [Sheet1$]')
SELECT TOP 5 ID, ItemName FROM dbo.MyItems
And return it to the clients:
SELECT * FROM OPENROWSET(BULK N'D:\1.xls', SINGLE_BLOB) rs
BUT, The file D:\1.xls
must exists already in advanced.
I have read this accepted answer: T-SQL: Export to new Excel file
4 If you dont want to create an EXCEL file in advance and want to export data to it, use
EXEC sp_makewebtask
sp_makewebtask
is deprecated.
Is there a way I could do this task without having an already existing file? or to at least dynamically create a suitable file before the export? or even better: not use any files at all?
Thanks.