Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server view Stored Procedures full relationships

I am working on a bunch of very complicated stored procedures on SQLSERVER 2012 Express databases using SQLSEVER 2012 Management studio.

For example I have a stored procedure SP1 calling another stored procedure SP2 and SP2 calls SP3, SP3 calls SP4 ...

What I would like to see is a diagram that can provide me the full relationships between these stored procedures and all the tables that being used in these stored procedures.

I have tried the view dependencies tools provided by SQL Server management studio, but sometimes it misses descendants stored procedures when I click "View Dependencies" of a selected stored procedure. Also it can't show a full picture of relationships between these stored procedures.

Is there any tool can help me with this? Or any suggestions on how to understand complicated relationship stored procedures?

like image 526
lengsiqi Avatar asked Jun 05 '15 15:06

lengsiqi


People also ask

How can I see all relationships in SQL Server?

Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose Relationships from the shortcut menu. In the Foreign Key Relationships dialog box, select the relationship with properties you want to view.

How do I view stored procedure permissions?

From Stored Procedure Properties, select the Permissions page. To grant permissions to a user, database role, or application role, select Search. In Select Users or Roles, select Object Types to add or clear the users and roles you want. Select Browse to display the list of users or roles.

How do I view a stored procedure in SQL Server?

Using SQL Server Management StudioExpand Stored Procedures, right-click the procedure and then select Script Stored Procedure as, and then select one of the following: Create To, Alter To, or Drop and Create To. Select New Query Editor Window. This will display the procedure definition.

How do I show many to many relationships in SQL?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.


2 Answers

I found following query quite useful, hope it can help.

select      
        distinct 
        ObjJectType=
        case Obj.xType
        When 'V' then
            'VIEW'
        When 'P' then
            'PROC'
        When 'D' then
            'INDEX'
        When 'TF' then
            'TABLE FUNCTION'
        When 'FN' then
            'FUNCTION'
        When 'U' then
            'TABLE'
        When 'F' then
            'FK CONSTRAINT'
        When 'PK' then
            'PK CONSTRAINT'
        When 'TR' then
            'TRIGGER'
        When 'S' then
            'SYSTEM OBJECT'
        end ,
        Obj.name
from 
        syscomments Com inner join sysobjects Obj
  on  Com.id = Obj.id
where 
        Com.text like '%sp_name%' 
order by Obj.name
like image 154
mhan0125 Avatar answered Oct 03 '22 15:10

mhan0125


You can build a recursive query around sys.objects joined on sys.sql_modules (the definition field contains the sproc code) and build yourself a dependencies tree.

like image 29
Ilan Avatar answered Oct 03 '22 15:10

Ilan