Skip to content

Instantly share code, notes, and snippets.

@adityadeshpande
Created November 2, 2020 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adityadeshpande/310212707a31a829e8f25959dfe37c68 to your computer and use it in GitHub Desktop.
Save adityadeshpande/310212707a31a829e8f25959dfe37c68 to your computer and use it in GitHub Desktop.
$SourceServer = "tcp:servername-s1.database.windows.net"
$SourceUserName = "username-u1@servername-s1"
$SourcePassword = "password-p1"
$TargetServer = "tcp:servername-s2.database.windows.net"
$TargetUserName = "username-u2@servername-s2"
$TargetPassword = "password-p2"
$database = "database-name" # Kept same for simplicity.
$bckDirectory = "D:\DataMigration\"
$tableNames = @(
'Table1',
'Table2',
'Table3', ...
) #Use "SELECT * FROM {database-name}.INFORMATION_SCHEMA.TABLES; GO" to get the tablenames
foreach ($table in $tableNames) {
$invocationName = $database + ".dbo." + $table
$datFilePath = $bckDirectory + $table + ".dat"
#Get Data
Write-Host "Getting data for " $invocationName "output file at " $datFilePath -ForegroundColor Yellow
bcp $invocationName out $datFilePath -c -U $SourceUserName -S $SourceServer -P $SourcePassword
#Insert Data
Write-Host "Inserting data into " $invocationName "from source file at " $datFilePath -ForegroundColor Yellow
bcp $invocationName in $datFilePath -c -U $TargetUserName -S $TargetServer -P $TargetPassword
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment