Created
August 13, 2024 03:29
-
-
Save nanoDBA/6db886ac5802560cdb5da58058a5b161 to your computer and use it in GitHub Desktop.
Loops through an array of tables, and exports each one to its own neatly named SQL file
This file contains hidden or 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
<# Table Export #> | |
$someTables = @" | |
TBL_A_FOO | |
TBL_B_BAR | |
TBL_C_BAZ | |
TBL_D_QUX | |
TBL_E_QUUX | |
TBL_F_CORGE | |
"@.split("`n").TrimEnd("`r") | |
$dbName = 'Tarfful' | |
$instanceName = 'Kashyyyk' | |
# Get current timestamp | |
$timestamp = (Get-Date).ToString("yyyyMMddHHmmss") | |
# Loop through each table and export data | |
foreach ($table in $someTables) { | |
$fileName = "${instanceName}-${timestamp}-script-${table}.sql" | |
$filePath = Join-Path -Path "z:\Wookiee" -ChildPath $fileName | |
# Define the splatted parameters for Get-DbaDbTable | |
$tableParams = @{ | |
SqlInstance = $instanceName | |
Database = $dbName | |
Table = $table | |
} | |
# Define the splatted parameters for Export-DbaDbTableData | |
$exportParams = @{ | |
FilePath = $filePath | |
NoPrefix = $true | |
} | |
# Execute the commands with splatted parameters | |
Get-DbaDbTable @tableParams | Export-DbaDbTableData @exportParams | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment