powershell sql-to-sql powershell bulkcopy import speedtest

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell sql-to-sql powershell bulkcopy import speedtest相关的知识,希望对你有一定的参考价值。

# Setup runspace pool and the scriptblock that runs inside each runspace
$pool = [RunspaceFactory]::CreateRunspacePool(1,5)
$pool.Open()
$jobs = @()

# This is the workhorse.
$scriptblock = {
	$source = "Data Source=sqlserver;Integrated Security=True;Connection Timeout=3;Initial Catalog=pssqlbulkcopy"
	$dest = "Data Source=sql2016;Integrated Security=True;Connection Timeout=3;Initial Catalog=pssqlbulkcopy"
	$conn = New-Object System.Data.SqlClient.SqlConnection
	$conn.ConnectionString = $source
	$conn.Open()
	$cmd = New-Object System.Data.SqlClient.SqlCommand
	$cmd.Connection = $conn
	$sql = "SELECT * from [pssqlbulkcopy].[dbo].[speedtest] WITH (SNAPSHOT)"
	$cmd.CommandText = $sql
	$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($dest)
	$bulkcopy.DestinationTableName = "speedtest"
	$bulkcopy.BatchSize = 1000000
	$bulkcopy.WriteToServer($cmd.ExecuteReader())
	$bulkcopy.Close()
	$conn.Close()
	return $error[0]
}

$elapsed = [System.Diagnostics.Stopwatch]::StartNew()

1..10 | foreach {
		$job = [PowerShell]::Create()
		$null = $job.AddScript($scriptblock)
		$job.RunspacePool = $pool
		$jobs += [PSCustomObject]@{ Pipe = $job; Status = $job.BeginInvoke() }
	}


while ($jobs.Status.IsCompleted -notcontains $true) {}
$secs = $elapsed.Elapsed

$errors = @()
foreach ($job in $jobs) { $results += $job.Pipe.EndInvoke($job.Status) ;$job.Pipe.Dispose() }
$elapsed

$pool.Close()

以上是关于powershell sql-to-sql powershell bulkcopy import speedtest的主要内容,如果未能解决你的问题,请参考以下文章

Python、Powershell 和 Popen

为自动执行设置 PowerShell 脚本

禁用多个计划任务 - PowerShell

查询结果设置为变量 - powershell

浅谈SQL2SQL翻译

浅谈SQL2SQL翻译