Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$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"
$sourcedatabase = "source-database-name"
$targetdatabase = "target-database-name"
$bckDirectory = "D:\DataMigration\"
$tableNames = @(
'Table1',
'Table2',
'Table3', ...
) #Use "SELECT * FROM {database-name}.INFORMATION_SCHEMA.TABLES; GO" to get the tablenames
foreach ($table in $tableNames) {
$datFilePath = $bckDirectory + $table + ".dat"
$sourceinvocationName = $sourcedatabase + ".dbo." + $table
#Get Data
Write-Host "Geting data for " $sourceinvocationName "output file at " $datFilePath -ForegroundColor Yellow
bcp $sourceinvocationName out $datFilePath -c -U $SourceUserName -S $SourceServer -P $SourcePassword
$targetinvocationName = $targetdatabase + ".dbo." + $table
#Insert Data
Write-Host "Inserting data into " $targetinvocationName "from source file at " $datFilePath -ForegroundColor Yellow
bcp $targetinvocationName 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
You can’t perform that action at this time.