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

Constructing nested array structs in BigQuery

How to decrypt stored password from SSMS registered servers

Cause for Parameter is incorrect 0x80070057 error in ssis