{"id":14021,"date":"2011-05-06T00:01:00","date_gmt":"2011-05-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/05\/06\/use-powershell-to-copy-a-table-between-two-sql-server-instances\/"},"modified":"2011-05-06T00:01:00","modified_gmt":"2011-05-06T00:01:00","slug":"use-powershell-to-copy-a-table-between-two-sql-server-instances","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-copy-a-table-between-two-sql-server-instances\/","title":{"rendered":"Use PowerShell to Copy a Table Between Two SQL Server Instances"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to quickly and efficiently copy a table between two SQL Server instances.<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>&nbsp; Hey, Scripting Guy! How can I copy a table between two SQL Server instances?<\/p>\n<p>&mdash;PY<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello PY, Microsoft Scripting Guy, Ed Wilson, is here. Today is the last day for this week&rsquo;s SQL Guest Blogger series. Today our guest is Yan Pan. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5008.yanpan_301482A2.jpg\"><img decoding=\"async\" height=\"215\" width=\"154\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5164.yanpan_thumb_4184E37A.jpg\" alt=\"Photo of Yan Pan\" border=\"0\" title=\"Photo of Yan Pan\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<blockquote>\n<p>Yan Pan has 10 years working on various versions of SQL Server (MCITP SQL Server 2008, MCITP SQL Server 2005, MCDBA SQL Server 2000, plus OCA Oracle 10g), mainly in the financial services sector. She is a senior database architect, and she wrote a Wrox book titled <a target=\"_blank\" href=\"http:\/\/books.internet.com\/books\/0470477288\">Microsoft SQL Server 2008 Administration with Windows PowerShell<\/a>. Besides SQL Server and Windows PowerShell, she also develops complex BI solutions with Microsoft technologies, works on other databases, such as Oracle and columnar databases, programs in .NET, and performs financial data development and research. If you would like to read more articles by her, visit her website at <a target=\"_blank\" href=\"http:\/\/yanpansql.com\/\">http:\/\/yanpansql.com\/<\/a>.<\/p>\n<\/blockquote>\n<p>Very often, a DBA gets user requests to copy a table from a production to a development environment for testing purposes, or to synchronize a table in a reporting database with a production database. As a DBA, you can create an SSIS package with a Data Flow task in Business Intelligence Development Studio (BIDS), or you can launch the Import and Export Wizard from SQL Server Management Studio and let the wizard create an SSIS package for you. <\/p>\n<p>However, you cannot reuse the same SSIS package to copy a different table with a different structure. You have to create a new package for the new table or modify the column mappings of an existing package in BIDS. It would be far more convenient to have a script that takes the names of the source server, source database, source table, destination server, destination database, and destination table as the parameters, and can be reused by simply providing different names. <\/p>\n<p>Scripting can make a DBA&rsquo;s life much easier. You might ask, &ldquo;Why not use the bcp utility?&rdquo; Yes, you can export the source table to a file and then import it to the destination table from the file. However, it requires creating a file on disk. We all know I\/O is typically the most expensive resource with data manipulation activities. Of course, you can also use non-Microsoft tools, such as SQL Data Compare from Red Gate, but these tools come at a cost, and they are not usually optimized for your special use cases. They can be slow and resource intensive when the target table size is big. <\/p>\n<p>Here I introduce a Windows PowerShell script that can be reused to copy the data from a source table to a destination table. This script uses the .NET <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.data.sqlclient.sqlbulkcopy.aspx\">SqlBulkCopy<\/a> class with a streaming <b>IDataReader<\/b> to achieve the optimal performance (using a single thread).<\/p>\n<p>Let us first define the parameters in the following param block. As you can see, the script requires a source server name, a source database name, a source table name, and a destination server name. The names of the destination database and table are optional. If you do not provide them, the script assumes that they are the same as the source. If you have a table in a non-default schema, you also need to specify the table names as two-part names. For example, if the user account under which you run the script maps to a user in the destination database with the default schema <b>dbo<\/b>, but you want to copy the data into an <b>Order<\/b> table in a non-default schema <b>Test<\/b>, then you need to specify the destination table name as <b>Test.Order<\/b>. The script also uses a switch parameter, <b>Truncate<\/b>. When this switch is included, the destination table is truncated and the existing data is removed before the copy.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span style=\"color: #0000ff\"><span>Param<\/span><\/span><\/span><span> (<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$SrcServer<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] $SrcDatabase,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$SrcTable<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestServer<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestDatabase<\/span><\/span>, # Name of the destination database is optional. When omitted, it is set to the source database name.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestTable<\/span><\/span>, # Name of the destination table is optional. When omitted, it is set to the source table name. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">switch<\/span><\/span>] <span><span style=\"color: #800080\">$Truncate<\/span><\/span> # Include this switch to truncate the destination table before the copy.<\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"color: #000000\"><span><span>&nbsp; <\/span><\/span><span>)<\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"color: #000000\">&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$DestDatabase<\/span><\/span>.<span><span style=\"color: #8b4513\">Length<\/span><\/span> &ndash;eq 0) {<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestDatabase <span><span style=\"color: #ff0000\">=<\/span><\/span> $SrcDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$DestTable<\/span><\/span>.<span><span style=\"color: #8b4513\">Length<\/span><\/span> &ndash;eq 0) {<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestTable <span><span style=\"color: #ff0000\">=<\/span><\/span> $SrcTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$Truncate<\/span><\/span>) { <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$TruncateSql <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800000\">&#8220;TRUNCATE TABLE &#8220;<\/span><\/span> <span><span style=\"color: #ff0000\">+<\/span><\/span> $DestTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Sqlcmd -S $DestServer -d $DestDatabase -Q $TruncateSql<\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"color: #000000\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<\/blockquote>\n<p>Next, we prepare a <b>SqlDataReader<\/b>, a forward-only stream of rows from the source table. In our example, we have an Order table in the AdventureWorks database on the source server, YYCenter. Notice that we also create the helper function <b>ConnectionString<\/b> to return a connection string based on a server name and a database name.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span style=\"color: #0000ff\"><span>Function<\/span><\/span><\/span><span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span>([<span><span style=\"color: #008080\">string<\/span><\/span>] $ServerName, [<span><span style=\"color: #008080\">string<\/span><\/span>] $DbName) <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>&#8220;Data Source=<span><span style=\"color: #800080\">$ServerName<\/span><\/span>;Initial Catalog=<span><span style=\"color: #800080\">$DbName<\/span><\/span>;Integrated Security=True;&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"color: #000000\">&nbsp;<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$SrcConnStr <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span> $SrcServer $SrcDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span><span>&nbsp; <\/span><span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> System.Data.SqlClient.SQLConnection(<span><span style=\"color: #800080\">$SrcConnStr<\/span><\/span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$CmdText <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800000\">&#8220;SELECT * FROM &#8220;<\/span><\/span> <span><span style=\"color: #ff0000\">+<\/span><\/span> $SrcTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SqlCommand<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> system.Data.SqlClient.SqlCommand(<span><span style=\"color: #800080\">$CmdText<\/span><\/span>, <span><span style=\"color: #800080\">$SrcConn<\/span><\/span>)<\/span><span><span>&nbsp; <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$SrcConn.<span><span style=\"color: #8b4513\">Open<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"color: #000000\"><span><span><span>&nbsp; <\/span><\/span><span>[<\/span><\/span><span>System.Data.SqlClient.SqlDataReader<span>] <\/span><\/span><\/span><span><span><span style=\"color: #800080\">$SqlReader<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #ff0000\">=<\/span><\/span><span><span style=\"color: #000000\"> <\/span><\/span><span><span style=\"color: #800080\">$SqlCommand<\/span><\/span><span><span style=\"color: #000000\">.<\/span><\/span><span><span style=\"color: #8b4513\">ExecuteReader<\/span><\/span><\/span><span><span style=\"color: #000000\">()<\/span><\/span><\/p>\n<\/blockquote>\n<p>We are now ready to do the bulk insert into the destination table.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Segoe\"><span style=\"color: #0000ff\">Try<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestConnStr <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span> $DestServer $DestDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$bulkCopy<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> <span><span style=\"color: #800000\">Data.SqlClient.SqlBulkCopy<\/span><\/span>(<span><span style=\"color: #800080\">$DestConnStr<\/span><\/span>, [System.Data.SqlClient.SqlBulkCopyOptions]::<span><span style=\"color: #8b4513\">KeepIdentity<\/span><\/span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$bulkCopy<\/span><\/span>.DestinationTableName <span><span style=\"color: #ff0000\">=<\/span><\/span> <\/span><span><span style=\"color: #800080\">$DestTable<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$bulkCopy.<span><span style=\"color: #8b4513\">WriteToServer<\/span><\/span>($sqlReader)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">Catch<\/span><\/span> [System.Exception]<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$ex<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$_<\/span><\/span>.Exception<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><b>Write-Host<\/b> <span><span style=\"color: #800080\">$ex<\/span><\/span>.<\/span><span><span style=\"color: #8b4513\">Message<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><b><span><span style=\"color: #5f9ea0\">Write-Host<\/span><\/span><\/b> &#8220;Table $SrcTable in $SrcDatabase database on $SrcServer has been copied to table $DestTable in $DestDatabase database on $DestServer&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp; <\/span><\/span><span><span>&nbsp;<\/span>$SqlReader.<span><span style=\"color: #8b4513\">close<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span>.<span><span style=\"color: #8b4513\">Close<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span>.<span><span style=\"color: #8b4513\">Dispose<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$bulkCopy.<span><span style=\"color: #8b4513\">Close<\/span><\/span>()<\/span><\/span><\/p>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/span><\/p><\/blockquote>\n<p>You can also set a batch size, for example, 1000 rows, or increase the default time-out of 30 seconds to 60 seconds by setting the properties of the <b>SqlBulkCopy<\/b> instance as shown here. <\/p>\n<blockquote>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$bulkCopy.BatchSize = 1000<\/span><\/span><\/span><\/p>\n<p class=\"Code\" style=\"line-height: normal;list-style-type: disc;margin: 0in 0in 0pt\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$bulkCopy.BulkCopyTimeout = 0<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Since <b>Try\/Catch\/Finally<\/b> block is only available in Windows PowerShell 2.0, we add the version tag into the script. <\/p>\n<blockquote>\n<p><span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Courier New\"><span style=\"color: #008000\">#requires -version 2.0<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Let us put all the pieces of the script together. Here is the complete script Copy-SqlTable.ps1, and it can also be found in the <a target=\"_blank\" href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/9db45c7b-b717-4823-8336-664d225f3ba8\">Script Repository<\/a>.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span><span style=\"font-family: Segoe\"><span>&nbsp; <\/span><\/span><\/span><span><span style=\"font-family: Courier New\"><span style=\"color: #008000\"><span>#requires -version 2.0<\/span><span><span>&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">Param<\/span><\/span> (<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$SrcServer<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] $SrcDatabase,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$SrcTable<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[parameter(Mandatory <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$true<\/span><\/span>)] <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestServer<\/span><\/span>,<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestDatabase<\/span><\/span>, # Name of the destination database is optional. When omitted, it is set to the source database name.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">string<\/span><\/span>] <span><span style=\"color: #800080\">$DestTable<\/span><\/span>, # Name of the destination table is optional. When omitted, it is set to the source table name. <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>[<span><span style=\"color: #008080\">switch<\/span><\/span>] <span><span style=\"color: #800080\">$Truncate<\/span><\/span> # Include this switch to truncate the destination table before the copy.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">Function<\/span><\/span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span>([<span><span style=\"color: #008080\">string<\/span><\/span>] $ServerName, [<span><span style=\"color: #008080\">string<\/span><\/span>] $DbName) <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>&#8220;Data Source=$ServerName;Initial Catalog=$DbName;Integrated Security=True;&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>########## Main body ############ <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$DestDatabase<\/span><\/span>.<span><span style=\"color: #8b4513\">Length<\/span><\/span> &ndash;eq 0) {<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestDatabase <span><span style=\"color: #ff0000\">=<\/span><\/span> $SrcDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$DestTable<\/span><\/span>.<span><span style=\"color: #8b4513\">Length<\/span><\/span> &ndash;eq 0) {<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestTable <span><span style=\"color: #ff0000\">=<\/span><\/span> $SrcTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">If<\/span><\/span> (<span><span style=\"color: #800080\">$Truncate<\/span><\/span>) { <\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$TruncateSql <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800000\">&#8220;TRUNCATE TABLE &#8220;<\/span><\/span> <span><span style=\"color: #ff0000\">+<\/span><\/span> $DestTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Sqlcmd -S $DestServer -d $DestDatabase -Q $TruncateSql<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$SrcConnStr <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span> $SrcServer $SrcDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span><span>&nbsp; <\/span><span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> System.Data.SqlClient.SQLConnection(<span><span style=\"color: #800080\">$SrcConnStr<\/span><\/span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$CmdText <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800000\">&#8220;SELECT * FROM &#8220;<\/span><\/span> <span><span style=\"color: #ff0000\">+<\/span><\/span> $SrcTable<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SqlCommand<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> system.Data.SqlClient.SqlCommand(<span><span style=\"color: #800080\">$CmdText<\/span><\/span>, <span><span style=\"color: #800080\">$SrcConn<\/span><\/span>)<\/span><span><span>&nbsp; <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>$SrcConn.<span><span style=\"color: #8b4513\">Open<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>[System.Data.SqlClient.SqlDataReader] <span><span style=\"color: #800080\">$SqlReader<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$SqlCommand<\/span><\/span>.<span><span style=\"color: #8b4513\">ExecuteReader<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span>&nbsp;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span style=\"color: #0000ff\">Try<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$DestConnStr <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #5f9ea0\">ConnectionString<\/span><\/span> $DestServer $DestDatabase<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$bulkCopy<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <b><span><span style=\"color: #5f9ea0\">New-Object<\/span><\/span><\/b> <span><span style=\"color: #800000\">Data.SqlClient.SqlBulkCopy<\/span><\/span>(<span><span style=\"color: #800080\">$DestConnStr<\/span><\/span>, [System.Data.SqlClient.SqlBulkCopyOptions]::<span><span style=\"color: #8b4513\">KeepIdentity<\/span><\/span>)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$bulkCopy<\/span><\/span>.DestinationTableName <span><span style=\"color: #ff0000\">=<\/span><\/span> <\/span><span><span style=\"color: #800080\">$DestTable<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$bulkCopy.<span><span style=\"color: #8b4513\">WriteToServer<\/span><\/span>($sqlReader)<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span><span><span style=\"color: #0000ff\">Catch<\/span><\/span> [System.Exception]<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$ex<\/span><\/span> <span><span style=\"color: #ff0000\">=<\/span><\/span> <span><span style=\"color: #800080\">$_<\/span><\/span>.Exception<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><b>Write-Host<\/b> <span><span style=\"color: #800080\">$ex<\/span><\/span>.<\/span><span><span style=\"color: #8b4513\">Message<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>Finally<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>{<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><b><span><span style=\"color: #5f9ea0\">Write-Host<\/span><\/span><\/b> &#8220;Table $SrcTable in $SrcDatabase database on $SrcServer has been copied to table $DestTable in $DestDatabase database on $DestServer&#8221;<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$SqlReader.<span><span style=\"color: #8b4513\">close<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span>.<span><span style=\"color: #8b4513\">Close<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span><span><span style=\"color: #800080\">$SrcConn<\/span><\/span>.<span><span style=\"color: #8b4513\">Dispose<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><\/span><span>$bulkCopy.<span><span style=\"color: #8b4513\">Close<\/span><\/span>()<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Segoe\"><span><span>&nbsp; <\/span><\/span><span>}<\/span><\/span><\/p>\n<\/blockquote>\n<p>I used the script to copy the Production.ProductInventory table in the AdventureWorks sample database on YYCenter to the Production.ProductInventory table in the AdventureWorks database on YYCenter\\SQL2008R2. The following image shows the output of the script on my computer.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.yanpan2jpg_661CC150.jpg\"><img decoding=\"async\" height=\"146\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0741.yanpan2jpg_thumb_1EC76B5E.jpg\" alt=\"Image of script output\" border=\"0\" title=\"Image of script output\" style=\"padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px\" \/><\/a><\/p>\n<p>Thanks, Yan Pan&mdash;that was an excellent article. <\/p>\n<p>Well, this concludes SQL Guest Blogger Week. Join me tomorrow for the Weekend Scripter as I kick off the 2011 Scripting Games wrap-up articles. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to quickly and efficiently copy a table between two SQL Server instances. &nbsp; Hey, Scripting Guy! How can I copy a table between two SQL Server instances? &mdash;PY Hello PY, Microsoft Scripting Guy, Ed Wilson, is here. Today is the last day for this week&rsquo;s SQL Guest Blogger [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[146,56,3,176,45,252],"class_list":["post-14021","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-guest-blogger","tag-scripting-guy","tag-sql-server","tag-windows-powershell","tag-yan-pan"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to quickly and efficiently copy a table between two SQL Server instances. &nbsp; Hey, Scripting Guy! How can I copy a table between two SQL Server instances? &mdash;PY Hello PY, Microsoft Scripting Guy, Ed Wilson, is here. Today is the last day for this week&rsquo;s SQL Guest Blogger [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=14021"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14021\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=14021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=14021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=14021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}