Skip to content

Instantly share code, notes, and snippets.

@thebentern
Last active December 24, 2015 21:52
Show Gist options
  • Save thebentern/4a3537bce8b2e3262005 to your computer and use it in GitHub Desktop.
Save thebentern/4a3537bce8b2e3262005 to your computer and use it in GitHub Desktop.
Dump all of the tables in a given database into CSVs
$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