Getting unique table using PowerShell and SQL SMO

When accessing tables property of database in SMO, tables are indexed using name (excluding schema)
This is problematic when using different schemas.
To access a table using unique name use the following code


$instance = $args[0] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server($instance)
$db=$srv.Databases["ExampleDatabase"]
$table = $db.Tables["ExampleTable"] # will get dbo schema, or random?
 
$table = $db.Tables | Where-Object {$_.Schema -eq "dbo" -and $_.Name -eq "ExampleTable"} #will get your unique table

Comments

Popular posts from this blog

SSIS: Set parent package variable from child package

How to decrypt stored password from SSMS registered servers

Insert bulk statement does not support recompile (SQL 2017)