Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Create relationships between entities using SMO in Powershell.

Reply
 
Old 09-21-2007   #1 (permalink)
NJC


 
 

Create relationships between entities using SMO in Powershell.

Hello,

I'm just wondering is there an
SMO object which I can use to create relationships between entities?

I'm guessing it’s something like Microsoft.SqlServer.Management.Smo.Index or
Microsoft.SqlServer.Management.Smo.IndexedColumn.
However I’ve been having a play and can't seem to work out what are
acceptable
Parameters which I can pass; the lack of intellisense is killing me

So effectively I have created two tables with their own PK and all I want to
do create a relationship between the AccessPerson and Test tables. Here is
my code:

# Initialise Pk for AccessPerson.
$index = New-Object Microsoft.SqlServer.Management.Smo.Index($apTable,
"PK_AccessPerson")
$index.IndexKeyType =
[Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey

$indexedColumn = New-Object
Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "AccessPersonID")

$index.IndexedColumns.Add($indexedColumn)

$apTable.Indexes.Add($index)


# Initialise PK for Test.
$index = New-Object Microsoft.SqlServer.Management.Smo.Index($testTable,
"PK_Test")
$index.IndexKeyType =
[Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey

$indexedColumn = New-Object
Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "TestID")

$index.IndexedColumns.Add($indexedColumn)

$testTable.Indexes.Add($index)

How to create referential integrity??? I'm convinced that this must be
achievable programmatically so any help would be very much appreciated!

Thanks,


My System SpecsSystem Spec
Old 09-21-2007   #2 (permalink)
RichS


 
 

RE: Create relationships between entities using SMO in Powershell.

Have a look at the microsoft.sqlserver.management.smo.foreignkey class

http://msdn2.microsoft.com/en-us/lib...oreignkey.aspx

i haven't got access to a SQL box at the moment so can't test it I'm afraid
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"NJC" wrote:
Quote:

> Hello,
>
> I'm just wondering is there an
> SMO object which I can use to create relationships between entities?
>
> I'm guessing it’s something like Microsoft.SqlServer.Management.Smo.Index or
> Microsoft.SqlServer.Management.Smo.IndexedColumn.
> However I’ve been having a play and can't seem to work out what are
> acceptable
> Parameters which I can pass; the lack of intellisense is killing me
>
> So effectively I have created two tables with their own PK and all I want to
> do create a relationship between the AccessPerson and Test tables. Here is
> my code:
>
> # Initialise Pk for AccessPerson.
> $index = New-Object Microsoft.SqlServer.Management.Smo.Index($apTable,
> "PK_AccessPerson")
> $index.IndexKeyType =
> [Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey
>
> $indexedColumn = New-Object
> Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "AccessPersonID")
>
> $index.IndexedColumns.Add($indexedColumn)
>
> $apTable.Indexes.Add($index)
>
>
> # Initialise PK for Test.
> $index = New-Object Microsoft.SqlServer.Management.Smo.Index($testTable,
> "PK_Test")
> $index.IndexKeyType =
> [Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey
>
> $indexedColumn = New-Object
> Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "TestID")
>
> $index.IndexedColumns.Add($indexedColumn)
>
> $testTable.Indexes.Add($index)
>
> How to create referential integrity??? I'm convinced that this must be
> achievable programmatically so any help would be very much appreciated!
>
> Thanks,
>
My System SpecsSystem Spec
Old 09-21-2007   #3 (permalink)
NJC


 
 

RE: Create relationships between entities using SMO in Powershell.

Hey Rich,

Thanks for info (again ). Thats all I needed to get it sorted!

Cheers again,


"RichS" wrote:
Quote:

> Have a look at the microsoft.sqlserver.management.smo.foreignkey class
>
> http://msdn2.microsoft.com/en-us/lib...oreignkey.aspx
>
> i haven't got access to a SQL box at the moment so can't test it I'm afraid
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "NJC" wrote:
>
Quote:

> > Hello,
> >
> > I'm just wondering is there an
> > SMO object which I can use to create relationships between entities?
> >
> > I'm guessing it’s something like Microsoft.SqlServer.Management.Smo.Index or
> > Microsoft.SqlServer.Management.Smo.IndexedColumn.
> > However I’ve been having a play and can't seem to work out what are
> > acceptable
> > Parameters which I can pass; the lack of intellisense is killing me
> >
> > So effectively I have created two tables with their own PK and all I want to
> > do create a relationship between the AccessPerson and Test tables. Here is
> > my code:
> >
> > # Initialise Pk for AccessPerson.
> > $index = New-Object Microsoft.SqlServer.Management.Smo.Index($apTable,
> > "PK_AccessPerson")
> > $index.IndexKeyType =
> > [Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey
> >
> > $indexedColumn = New-Object
> > Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "AccessPersonID")
> >
> > $index.IndexedColumns.Add($indexedColumn)
> >
> > $apTable.Indexes.Add($index)
> >
> >
> > # Initialise PK for Test.
> > $index = New-Object Microsoft.SqlServer.Management.Smo.Index($testTable,
> > "PK_Test")
> > $index.IndexKeyType =
> > [Microsoft.SqlServer.Management.Smo.IndexKeyType]:riPrimaryKey
> >
> > $indexedColumn = New-Object
> > Microsoft.SqlServer.Management.Smo.IndexedColumn($index, "TestID")
> >
> > $index.IndexedColumns.Add($indexedColumn)
> >
> > $testTable.Indexes.Add($index)
> >
> > How to create referential integrity??? I'm convinced that this must be
> > achievable programmatically so any help would be very much appreciated!
> >
> > Thanks,
> >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Create new volume in Powershell PowerShell
Managing 2 activesync relationships .NET General
Create Printers With Powershell PowerShell
Create IIsWebDirectorySetting from PowerShell via WMI PowerShell
Better understanding WMP11 and UPNP relationships Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46