Please wait,
Processing your request...

    0%
  Business logo VanSoest.it  
  ... | Selecteer de Nederlandse taal | Print this page. | Linkedin page of Johan van Soest | User: Guest | Login

Postcard image. Click this to mail to Johan

WebHalla
 Content
  Management
   System


ICT-Hotlist Topic

Back to the ICT-Hotlist...
Don't miss the latest updates

How to export a table from Microsoft SQL server 2008r2 to Excel using PowerShell

Before PowerShell you had to use the Microsoft SQL Query Analyzer to export data to Excel (or query the database from Excel when allowed). This tip compares a simple query using SQL Query Analyzer versus PowerShell.
The example: get all the records of the table Person.Contact from the AdventureWorks sample database.

Standard Query report

The data can be extracted using the Microsoft SQL Server Management Studio or Microsoft SQL Query Analyzer by running the query:
-- ****************************************************************************
-- * This script displays all records / tuples of the table [Person].[Contact]
-- * from the Microsoft SQL Server sample database AdventureWorks
-- * (C)Copyright 2012 - 2019 Johan van Soest http://www.vansoest.it
-- ****************************************************************************

SELECT * FROM [AdventureWorks].[Person].[Contact]
Make sure you click the "Results to File" output selection button before running this script.
Save the Report file under the name you select. Next you have to import and format the Report output file for use in Excel.

PowerShell Query version

When using Microsoft SQL Server 2008r2 Management Studio, right click anywhere on the database tree and select "Start PowerShell" as can be seen in the next image:
How to open a Microsoft SQL Server PowerShell command window
Start a Microsoft SQL Server PowerShell command window

This opens a SQL Server PowerShell window that will accept commands right away.The following instruction will:
  • Query the database AdventureWorks
  • Selects all the records/tuples from the table Person.Contact
  • Redirects the query output to the Comma-Separated Values (CSV) export commandlet (Cmdlet)
  • This rewrites the file c:\Share\AdventureWorks.Person.Contact.CSV
  • The output type is formatted in Unicode.
Invoke-Sqlcmd -Query "SELECT * FROM [AdventureWorks].[Person].[Contact]" | Export-Csv c:\Share\AdventureWorks.Person.Contact.CSV -encoding "unicode"
The file "AdventureWorks.Person.Contact.CSV" is placed on the shared folder "c:\Share" on the SQL server and can be opened right away using Microsoft Excel 2007 or newer.
Presentation of table AdventureWorks.person.contact in Microsoft Excel
Presentation of the table Person.Contact from the AdventureWorks sample database in Microsoft Excel

The SQL Server PowerShell command window closes by typing "exit".
This example can be extended to more complex queries such as joins and where filters.

PowerShell command line version

You can also run Microsoft SQL Server PowerShell scripts on the Command line on a machine where the SQL Server PowerShell plugins are installed. The PowerShell modules are installed by default when installing Microsoft SQL Server. You can manually install the PowerShell Extensions for SQL Server 2012, 2014 or 2016 by installing the following components from the Microsoft® SQL Server® 2016 Feature Pack:
  • Microsoft® System CLR Types for Microsoft SQL Server® 2016 (SQLSysClrTypes.msi)
  • Microsoft® SQL Server® 2016 Shared Management Objects (SharedManagementObjects.msi)
  • Microsoft® Windows PowerShell Extensions for Microsoft SQL Server® 2016 (PowerShellTools.msi)
#############################################################################
# This script displays all records / tuples of the table [Person].[Contact]
# from the Microsoft SQL Server sample database AdventureWorks running on
# this computer (localhost) using PowerShell on the command line
# (C)Copyright 2016 - 2019 Johan van Soest http://www.vansoest.it
#############################################################################

# Load the Microsoft SQL-Server snap in modules

Add-PSSnapin SQLServer*

# Setup the query statement
$Query = "SELECT * FROM [AdventureWorks].[Person].[Contact]"
$DataBase = "AdventureWorks"
$ServerInstance = "localhost"

# Open the database connection, get a result set and save it as a Unicode CSV file.
Invoke-Sqlcmd -Query $Query -Database $DataBase -ServerInstance $ServerInstance |
    Export-Csv c:\Share\AdventureWorks.Person.Contact.CSV -encoding "unicode"
You may vote your opinion about this article:


Topic created  :  01-05-2017
Topic last edited  :  01-05-2017

Scripts and programming examples disclaimer

Unless stated otherwise, the script sources and programming examples provided are copyrighted freeware. You may modify them, as long as a reference to the original code and hyperlink to the source page is included in the modified code and documentation. However, it is not allowed to publish (copies of) scripts and programming examples on your own site, blog, vlog, or distribute them on paper or any other medium, without prior written consent.
Many of the techniques used in these scripts, including but not limited to modifying the registry or system files and settings, impose a risk of rendering the Operating System inoperable and loss of data. Make sure you have verified full backups and the associated restore software available before running any script or programming example. Use these scripts and programming examples entirely at your own risk. All liability claims against the author in relation to material or non-material losses caused by the use, misuse or non-use of the information provided, or the use of incorrect or incomplete information, are excluded. All content is subject to change and provided without obligation.
Generated by WebHalla™ Version 0.1.e.2 : Tuesday 22-1-2019 © Copyright 1995-2019 Johan van Soest
Response Form    Cookie- and Privacy statement
Weather in Waalre by OpenWeatherMap logo mist
Temperature -3.76 °C mist
Humidity 85 % mist
Air pressure 1017 hPa mist
Wind speed 2.1 m/s mist
Wind direction South South mist