Transfer Tables to a Different Schema via Powershell

Quick script to script out the transfer of tables belonging to on schema to the dbo schema.  This does no checking to see if the object exists in the dbo schema already, nor does it actually transfer the object.  It simply generates the script to do so.  I’m working on a more thorough version that will do all this, but I had a need for a quick-one off today.  This uses SQLPSX.

Import-Module SqlServer

Get-SqlServer -sqlserver 'ServerName' | %{
	Get-SqlDatabase -sqlserver $_ -dbname 'DatabaseName' | %{
		Get-SqlTable -Database $_ | where{$_.Schema -eq 'OrigSchema'} |%{
			"ALTER SCHEMA dbo TRANSFER [OrigSchema].[$($_.Name)]"
		}
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: