How to obtain the object dependency of the current database _ MySQL

Source: Internet
Author: User
How to obtain the source of the object dependency example of the current database CodeFor more information, see the following:

Create function udf_GenLevelPath ()
Returns @ v_Result table (LevelPath int, OName sysname)
/*************************************** *************************/
/* Function description: lists dependencies.DatabaseObject*/
/* Input parameter: None */
/* Output parameters: database object tables arranged by dependency. no dependency exists */
/* Write: anna */
/* Time: 2007-12-12 */
/*************************************** *************************/
As
Begin
Declare @ vt_ObjDepPath table (LevelPath int, OName sysname null)
Declare @ vt_Temp1 table (OName sysname null)
Declare @ vt_Temp2 table (OName sysname null)
-- Dependency level. the smaller the value, the stronger the dependency.
Declare @ vi_LevelPath int
Set @ vi_LevelPath = 1
-- Get all objects, excluding system objects
Insert into @ vt_ObjDepPath (LevelPath, OName)
Select @ vi_LevelPath, o. name
From sysobjects o
Where xtype not in ('s ', 'x ')

-- Get the name of the dependent object
Insert into @ vt_Temp1 (OName)
Select distinct object_name (sysdepends. depid)
From sysdepends, @ vt_ObjDepPath p
Where sysdepends. id <> sysdepends. depid
And p. OName = object_name (sysdepends. id)

-- Cyclic processing: Get the dependent object from the object
While (select count (*) from @ vt_Temp1)> 0
Begin
Set @ vi_LevelPath = @ vi_LevelPath + 1

Update @ vt_ObjDepPath
Set LevelPath = @ vi_LevelPath
Where OName in (select OName from @ vt_Temp1)
And LevelPath = @ vi_LevelPath-1

Delete from @ vt_Temp2

Insert into @ vt_Temp2
Select * from @ vt_Temp1

Delete from @ vt_Temp1

Insert into @ vt_Temp1 (OName)
Select distinct object_name (sysdepends. depid)
From sysdepends, @ vt_Temp2 t2
Where t2.OName = object_name (sysdepends. id)
And sysdepends. id <> sysdepends. depid

End

Select @ vi_LevelPath = max (LevelPath) from @ vt_ObjDepPath

-- Modify the maximum object level of the non-dependent object
Update @ vt_ObjDepPath
Set LevelPath = @ vi_LevelPath + 1
Where OName not in (select distinct
Object_name (sysdepends. id) from sysdepends)
And LevelPath = 1

Insert into @ v_Result
Select * from @ vt_ObjDepPath order by LevelPath desc
Return
End
Go

-- Call method
Select * from dbo. udf_GenLevelPath ()
Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.