-1

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

7
  • Can you use SQL scripts to copy specific tables from SQL DB in one VM to another without using DevOps? What's your VM? Azure VMs? Commented Apr 29, 2024 at 9:50
  • its good if you give me powershell script to copy, yeah those are azure VM's Commented May 1, 2024 at 6:49
  • To copy tables from one DB to the DB in another server, you need to add your target server as a linked server of your source server. Then run SQL scripts to copy, for example SELECT * INTO targetTable FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]. You can refer to these questions stackoverflow.com/questions/603502/…. Commented May 1, 2024 at 9:33
  • stackoverflow.com/questions/11009189/… and stackoverflow.com/questions/8285256/… Commented May 1, 2024 at 9:34
  • Can you check the PowerShell script mentioned in this documnet. Commented May 3, 2024 at 9:15

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.