Skip to content

Instantly share code, notes, and snippets.

@lrhache
Last active March 11, 2024 20:06
Show Gist options
  • Save lrhache/6e644018c417bb22007e to your computer and use it in GitHub Desktop.
Save lrhache/6e644018c417bb22007e to your computer and use it in GitHub Desktop.
Export all tables from a database to CSV files - AWS MSSQL 2008 / Win 2008
set-alias installutil $env:windir\microsoft.net\framework\v2.0.50727\installutil
installutil -i "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\Redist\Microsoft.SqlServer.Management.PSProvider.dll"
installutil -i "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\Redist\Microsoft.SqlServer.Management.PSSnapins.dll"
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
$SERVER_NAME="<SERVER NAME>"; $DB_NAME="<DB NAME>";$Tables = invoke-sqlcmd -query "SELECT name FROM sys.tables order by name" -database $DB_NAME -serverinstance $SERVER_NAME;foreach ($Table in $Tables){$TableName = $Table["name"];write-host -ForegroundColor Green "Creating File $TableName.csv";invoke-sqlcmd -query "SELECT * FROM $TableName" -database $DB_NAME -serverinstance $SERVER_NAME |export-csv -path D:\dump\$TableName.csv;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment