Export Data From SQL Server to Microsoft Excel Datasheet


/ Published in: SQL
Save to your folder(s)

How to Export Data From SQL Server to Microsoft Excel Datasheet


Copy this code and paste it in your HTML
  1. --Enable Ad Hoc Distributed Queries. Run following code in SQL Server Management Studio – Query Editor.
  2. EXEC sp_configure 'show advanced options', 1;
  3. GO
  4. RECONFIGURE;
  5. GO
  6. EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
  7. GO
  8. RECONFIGURE;
  9. GO
  10. --Create Excel Spreadsheet in root directory c:\contact.xls (Make sure you name it contact.xls). Open spreadsheet, on the first tab of Sheet1, create two columns with FirstName, LastName. Alternatively you can download sample Spreadsheet from here.
  11.  
  12. --Run following code in SQL Server Management Studio – Query Editor.
  13. USE [AdventureWorks];
  14. GO
  15. INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\contact.xls;',
  16. 'SELECT * FROM [Sheet1$]')
  17. SELECT TOP 5 FirstName, LastName
  18. FROM Person.Contact
  19. GO
  20. --Open contact.xls spreadsheet you will see first five records of the Person.Contact inserted into the first two columns.
  21. --Make sure your spreadsheet is closed during this operation. If it is open it may thrown an error. You can change your spreadsheet name as well name of the Sheet1 to your desired name.

URL: http://blog.sqlauthority.com/2008/01/08/sql-server-2005-export-data-from-sql-server-2005-to-microsoft-excel-datasheet/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.