Solved

How to List SQL Server Table Relationships

Posted on 2002-03-19
3
300 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

860 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