Run query to copy table data from server to server without linked server by using powershell

Mario2286 441 Reputation points
2023-08-18T08:29:53.6333333+00:00

I would like to copy table data from a server to different server without using linked server, i want to use powershell but i m getting some error when executing this scripts below . We have few database in source server which have same table and database start with name dam. eg dam_1, dam_2 . I need to copy all table data of dam database to destination server database table .

Query copy.sql which i m using to copy data is 'insert into select from' query

I believe something wrong with this powershell scripts below

$tableName = "dbo.t_glory"

$tableName2 = "dbo.t_must_glory

$SQLServer = "DB15","DB21"

$query = "C:\queryset\copy.sql"

$data = "select name from sys.databases where name like '%dam%'"

$dataset = Invoke-DbaQuery -SqlInstance $SQLServer -Database $data -Query $query -As DataSet

$dataset | Write-DbaDbTableData -SqlInstance 'DB6' -Database mesh -Table $tableName2

User's image

Windows for business | Windows Server | User experience | PowerShell
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Javier Villegas 905 Reputation points MVP
    2023-08-18T11:32:41.4466667+00:00

    Hello

    in your script your are missing a closing double-quote

    $tableName2 = "dbo.t_must_glory**"**

    here is the full script. it should work

    $tableName = "dbo.t_glory"

    $tableName2 = "dbo.t_must_glory"

    $SQLServer = "DB15","DB21"

    $query = "C:\queryset\copy.sql"

    $data = "select name from sys.databases where name like '%dam%'"

    $dataset = Invoke-DbaQuery -SqlInstance $SQLServer -Database $data -Query $query -As DataSet

    $dataset | Write-DbaDbTableData -SqlInstance 'DB6' -Database mesh -Table $tableName2

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.