Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
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
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 15, 2016 5:07 AM
Points: 507, Visits: 1,251
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


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, February 24, 2016 7:59 AM
Points: 1,556, Visits: 5,007
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
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, September 15, 2016 5:07 AM
Points: 507, Visits: 1,251
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