Get table's unique indexes

  • Is there a way to list the unique table constraints on a given table using smo/powershell?

    Or do I have to query the system views?

    Thanks!

  • Would something like this work?

    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null

    $s = new-object ("Microsoft.SqlServer.Management.Smo.Server") "SERVERNAME"

    $d = $s.Databases | ?{$_.Name -eq "Sandbox"}

    $t = $d.Tables | ?{$_.Name -eq "Table1"}

    $t.Indexes | ?{$_.IsUnique} | Select Name

    - Jeff

  • That does get them, but also returns the PK, which in my case I want to ignore.

    I was hoping there was some property or combination of properties in SMO.... And I just found it:

    $t.Indexes | ? {$_.IndexKeyType -eq "DriUniqueKey"}

    Thanks!

    Paul

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply