I want to copy the specific tables of SQL data base from sorce VM to the destination VM's data base using azure Devops pipeline, can anyone suggest the way to do it
1 Answer
its good if you give me powershell script to copy,
You can use below PowerShell script to copy the tables of sql database from one VM to another VM means from one sql server to another sql server:
Param (
[parameter(Mandatory = $true)]
[string] $SrcServer,
[parameter(Mandatory = $true)]
[string] $SrcDatabase,
[parameter(Mandatory = $true)]
[string] $DestServer,
[parameter(Mandatory = $true)]
[string] $DestDatabase,
[parameter(Mandatory = $false)]
[string] $LogLocation = "C:\Temp\"
)
$ScriptLog = $LogLocation+$SrcDatabase+"_"+$DestDatabase+".log"
Try
{
Get-Date >> $ScriptLog
}
Catch [System.Exception]
{
$ex = $_.Exception
Write-Host $ex.Message
}
Try
{
$TableNames = Read-SQLTableData -ServerInstance $SrcServer -DatabaseName $SrcDatabase -SchemaName "dbo" -TableName "TableList"
}
Catch [System.Exception]
{
$ex = $_.Exception
Write-Host $ex.Message
}
ForEach($TableName in $TableNames)
{
Try
{
Write-Host $TableName.TableName
Copy-DbaDbTableData -SQLInstance $SrcServer -Destination $DestServer -Database $SrcDatabase -DestinationDatabase $DestDatabase -Table $TableName.TableName -DestinationTable $TableName.TableName >> $ScriptLog
}
Catch [System.Exception]
{
$ex = $_.Exception
Write-Host $ex.Message
}
}
For more information you can refer to this document.
SELECT * INTO targetTable FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]. You can refer to these questions stackoverflow.com/questions/603502/….