Solved

How to List SQL Server Table Relationships

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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