Solved

How to List SQL Server Table Relationships

Posted on 2002-03-19
3
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 34
Finding Where Clause Value in SQL Views and SP 21 38
Split string into 3 separate fields 5 20
SQL profiler 3 12
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

756 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