SQL Server has several tools for exporting and importing data. The simple utilities like the T-SQL statement BULK...
INSERT or the BCP utility does the data transfer by using flat text files. In order to deal with any other type of file to be exported or imported we must construct a DTS package using the DTS designer or the DTS wizard. This will eventually create a DTS package that is an independent object that we must test, maintain and handle. The work of transferring data becomes more complicated even if we want to transfer just a simple Excel file to the file system.
Here I suggest a simple but useful T-SQL stored procedure that does the export to Excel by using the linked server technology.
In order to achieve the export, you must first create an empty Excel file with a fixed name and place on the server. I called it Empty.xls and placed it in the c:\temp directory. This file is not to be deleted and acts like a template for the target Excel file before it is populated by data.
The Empty.xls file is built with a single spread sheet called ExcelTable and the first (and only) line in the file consists of the letters A,B,C,...Z. These letters act like the column names of the Excel table. That means that up to 26 columns can be exported in a single query. (The given stored procedure code can be altered to support more columns in the result set. Simply write F1, F2 ,F3... in the Excel template and change the column list in the procedure to reflect the change.)
The T-SQL stored procedure, sp_write2Excel, gets the target Excel file name and path, the number of columns in the result set and the T-SQL query. The query should use the convert function for every non-string exported column since the resulting Excel cells are actually strings.
The procedure copies the empty.xls template file to the new target Excel file. Then it builds a linked server to that file and uses dynamic T-SQL to construct and Insert/Select statements that write the data to the Excel file.
Here is the procedure code:
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:\temp\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 GO
Example for procedure usage:
Use master go exec sp_write2Excel -- Target Excel file 'c:\temp\NorthProducts.xls' , -- Number of columns in result 3, -- The query to be exported 'select convert(varchar(10),ProductId), ProductName, Convert (varchar(20),UnitPrice) from Northwind..Products'
In conclusion, the procedure can be used as a general tool for exporting data to an Excel spreadsheet since the bcp utility can only export to a text file.
The usage of this procedure can substitute the usage of DTS package designer or the DTS wizard in order to export data to excel each time such an action is required.
About the author
Eli Leiba (e-mail: [email protected]) works at the Israel Electric Company as a Senior Application DBA in Oracle and MS SQL Server. He also has certifications from Microsoft and BrainBench in Oracle and SQL Server database administration and implementation. Mr. Leiba holds a B.S. in Computer Science since 1991 and has 13 years' experience working in the databases field. Additionally Mr. Leiba teaches SQL Server DBA and Development courses at Microsoft CTEC and also serves as a senior database consultant for several Israeli start-up companies.