私のDESTテーブルには自分のSOURCEサーバからの選択に列挙されているが、DESTテーブルにはより多くの列がある場合に動作するPowerShellスクリプトがあります。私が挿入したいdestテーブルから列を指定する方法の例を示すものは何も見つかりませんでした。 SourceServerとDestServerはリンクサーバーではありません。Powershellで異なるSQLソースとdest接続を持つリストに挿入を指定します。
Param (
#[parameter(Mandatory = $true)]
[string] $SrcServer = "SourceServer",
[parameter(Mandatory = $true)]
[string] $SrcDatabase = "SourceDb",
#[parameter(Mandatory = $true)]
[string] $SrcTable = "stage.InternalNotes",
#[parameter(Mandatory = $true)]
[string] $DestServer = "DestServer",
#[parameter(Mandatory = $true)]
[string] $DestDatabase = "DestDb",
[parameter(Mandatory = $true)]
[string] $DestTable = "dbo.InternalNotes",
)
Function ConnectionString([string] $ServerName, [string] $DbName)
{
"Data Source=$ServerName;Initial Catalog=$DbName;Integrated Security=True;User ID=$UID;Password=$PWD;"
}
$SrcConnStr = ConnectionString $SrcServer $SrcDatabase
$SrcConn = New-Object System.Data.SqlClient.SQLConnection($SrcConnStr)
$CmdText = "SELECT
ino.UserId
,ino.StoreId
,ino.PostedById
,ino.DatePosted
,ino.NoteSubject
,ino.NoteText
,ino.NoteType
,ino.Classify
,ino.CreatedBy
,ino.CreatedUtc
,IsReadOnly = 0
FROM
stage.InternalNotes AS ino
"
$SqlCommand = New-Object system.Data.SqlClient.SqlCommand($CmdText, $SrcConn)
$SrcConn.Open()
[System.Data.SqlClient.SqlDataReader] $SqlReader = $SqlCommand.ExecuteReader()
Try
{
$DestConnStr = ConnectionString $DestServer $DestDatabase
$bulkCopy = New-Object Data.SqlClient.SqlBulkCopy($DestConnStr, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity)
$bulkCopy.DestinationTableName = $DestTable
$bulkCopy.WriteToServer($sqlReader)
}
Catch [System.Exception]
{
$ex = $_.Exception
Write-Host $ex.Message
}
Finally
{
Write-Host "Table $SrcTable in $SrcDatabase database on $SrcServer has been copied to table $DestTable in $DestDatabase database on $DestServer"
$SqlReader.close()
$SrcConn.Close()
$SrcConn.Dispose()
$bulkCopy.Close()
}
基本的に、私はこれを行うことができるようする必要があります:それは好きではなかったいくつかの理由について
:すべては受け入れ答えに基づいて動作するようになった後
INSERT INTO dbo.InternalNotes --DEST Server table
(
userID
,StoreID
,PostedByID
,DatePosted
,NoteSubject
,NoteText
,NoteType
,Classify
,CreatedBy
,CreatedDateUTC
,IsReadOnly
)
SELECT
ino.UserId
,ino.StoreId
,ino.PostedById
,ino.DatePosted
,ino.NoteSubject
,ino.NoteText
,ino.NoteType
,ino.Classify
,ino.CreatedBy
,ino.CreatedUtc
,IsReadOnly = 0
FROM
stage.InternalNotes AS ino --SOURCE Server table
編集をライン:
$bulkCopy = New-Object -TypeName Data.SqlClient.SqlBulkCopy -ArgumentList $DestSqlConnection, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity, $DestSqlTransaction;
それがエラーを与えた:
Cannot convert argument "1", with value: "[System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity", for "SqlBulkCopy" to type "System.Data.SqlClient.SqlBulkCopyOptions": "Cannot convert value "[System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity" to type "System.Data.SqlClient.SqlBulkCopyOptions". Error: "Unable to match the identifier name [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity to a valid enumerator name. Specify one of the following enumerator names and try again: Default, KeepIdentity, CheckConstraints, TableLock, KeepNulls, FireTriggers, UseInternalTransaction, AllowEncryptedValueModifications""
だから代わりに、私はこれにそれを変更し、すべてが働いた:
$bulkCopy = New-Object Data.SqlClient.SqlBulkCopy($DestSqlConnection, [System.Data.SqlClient.SqlBulkCopyOptions]::KeepIdentity,$DestSqlTransaction)
詳細な回答ありがとうございます!私のPowershellでの私の唯一の経験は、私のGoogle検索で見つけられるものです。このスクリプトは、ほとんどがhttps://blogs.technet.microsoft.com/heyscriptingguy/2011/05/06/use-powershell-to-copy-a-table-between-two-sql-server-instances/に基づいていました。これらのタイプのプロジェクトはSSISでセットアップされていますが、私たちのサーバーはかなり古くなっています(2012年は私が信じています)、私のプロジェクトはすべて2015年です。下位バージョンでパッケージを再作成するより簡単な方法を探しています。私は来週あなたの提案を試してみましょう! – Doolius
カップルで微調整して、魅力的に機能しています! (変更されたものに関するコメントを追加するために私の質問を編集する)もう一度ありがとう! – Doolius