|
|
|
|||||||
|
ICT-Hotlist TopicHow to export a table from Microsoft SQL server 2008r2 to Excel using PowerShellBefore 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 reportThe data can be extracted using the Microsoft SQL Server Management Studio or Microsoft SQL Query Analyzer by running the query:
-- ****************************************************************************
Make sure you click the "Results to File" output selection button before running this script.
-- * This script displays all records / tuples of the table [Person].[Contact] -- * from the Microsoft SQL Server sample database AdventureWorks -- * (C)Copyright 2012 - 2024 Johan van Soest http://www.vansoest.it -- **************************************************************************** SELECT * FROM [AdventureWorks].[Person].[Contact] 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 versionWhen 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: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:
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 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 versionYou 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:
#############################################################################
# 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 - 2024 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:
Scripts and programming examples disclaimerUnless 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. |