?
Solved

How to List SQL Server Table Relationships

Posted on 2002-03-19
3
Medium Priority
?
305 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 800 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

770 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