Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Get table's unique indexes Expand / Collapse
Author
Message
Posted Wednesday, October 10, 2012 9:58 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: 2 days ago @ 6:36 AM
Points: 402, Visits: 859
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!




Post #1371025
Posted Wednesday, October 10, 2012 3:29 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 7:28 AM
Points: 1,281, Visits: 4,731
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
Post #1371141
Posted Thursday, October 11, 2012 5:10 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: 2 days ago @ 6:36 AM
Points: 402, Visits: 859
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



Post #1371361
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse