Last active
December 24, 2015 21:52
-
-
Save thebentern/4a3537bce8b2e3262005 to your computer and use it in GitHub Desktop.
Dump all of the tables in a given database into CSVs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$database = 'MyDatabaseName' | |
$instance = 'localhost\SQLEXPRESS' | |
$tables = Invoke-SqlCmd -Query "SELECT TABLE_NAME FROM [$database].INFORMATION_SCHEMA.Tables" -ServerInstance $instance -Database 'msdb' | |
foreach ($table in $tables) { | |
$tableName = $table.TABLE_NAME | |
Invoke-SqlCmd -Query "SELECT TOP 50 * FROM [$tableName]" -ServerInstance $instance -Database $database | | |
ConvertTo-Csv -Delimiter '|' -NoType | | |
ForEach-Object {$_.Replace('"','')}| | |
Out-file "C:\DATA\$tableName.csv" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment