Solved

How to List SQL Server Table Relationships

Posted on 2002-03-19
3
297 Views
Last Modified: 2010-08-05
I use the following VBA code to list the table relationships in an Access database:

Public Sub ListAccessRelations(ByVal vstrDatabase As String)

    Dim db As Database
    Dim rel As DAO.Relation
    Dim fld As DAO.Field
    Set db = DBEngine.Workspaces(0).OpenDatabase(vstrDatabase)
    For Each rel In db.Relations
        For Each fld In rel.Fields
            Debug.Print rel.Table, rel.ForeignTable, CBool(rel.Attributes And dbRelationUpdateCascade), CBool(rel.Attributes And dbRelationDeleteCascade), fld.Name, fld.ForeignName
        Next fld
    Next rel
    db.Close
    Set db = Nothing
   
End Sub

How do I do the same thing for a SQL Server 2000 database? I've tried the following code, but can't seem to get the last piece:

Public Sub ListSQLRelations _
    ( _
        ByVal vstrSQLServerName As String, _
        ByVal vstrDatabaseName As String _
    )

    Dim varColumn As Variant
    Dim SQLColumn As SQLDMO.Column
    Dim SQLDatabase As SQLDMO.Database
    Dim SQLKey As SQLDMO.Key
    Dim SQLServer As SQLDMO.SQLServer
    Dim SQLTable As SQLDMO.Table
   
    Set SQLServer = New SQLDMO.SQLServer
    SQLServer.Connect vstrSQLServerName, "sa", ""
    Debug.Print "connected to " & vstrSQLServerName
    Set SQLDatabase = SQLServer.Databases(vstrDatabaseName)
    Debug.Print "connected to " & SQLDatabase.Name
    For Each SQLTable In SQLDatabase.Tables
        If Not SQLTable.SystemObject Then
                For Each SQLKey In SQLTable.Keys
                    For Each varColumn In SQLKey.KeyColumns
                        Debug.Print SQLTable.Name, SQLKey.Type, varColumn
                    Next varColumn
                Next SQLKey
        End If
    Next SQLTable
    Debug.Print "done"
   
End Sub
0
Comment
Question by:raykata2ddotcom
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
JamesT earned 200 total points
ID: 6881176
If you are trying to get a listing of the foreign key relationships look at the stored procedure sp_fkeys.

With it you can call it in the following manner:

EXEC sp_fkeys @pktable_name = @TABLE_NAME

EXEC sp_fkeys @pktable_name = 'TABLE_NAME'

From MS SQL Books On-Line:  Returns logical foreign key information for the current environment. This procedure shows foreign key relationships including disabled foreign keys.

0
 

Author Comment

by:raykata2ddotcom
ID: 6881281
Awesome - next to no code to write !!!!

Just for my educationon the SQLDMO object model, I'd still like to know how to do this in code.

Thx

0
 
LVL 2

Expert Comment

by:JamesT
ID: 6881309
Well, you will have to loop through the sysobjects table looking for the relationship between ID and PARENTID for the table you are curious about. For example table SYSOBJECTS has an ID of 1 with a NAME of CUSTOMERS. Table SYSOBJECTS has another row with ID of 2, a NAME of FK_ORDER_CUSTOMER and a PARENTID or 1. You get the idea. You have parent child relations between tables by way of FK relationships.

Something to that effect.

If I am wrong I reserve the right to beg for mercy.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now