Query database object Dependencies

Source: Internet
Author: User
USE TestDB1
GO
Select distinct OBJECT_NAME (object_id) as objname
FROM sys. SQL _dependencies
WHERE referenced_major_id = OBJECT_ID ('dbo. tblproject ')
Order by objname

Although the above Code can be used from sys. SQL _dependencies directly searches for the reference relationship of the SQL object, but this method can only find the direct reference relationship in a single database, cross-database reference and reference generated by dynamic SQL are not found in this way. However, you can use the text search method to traverse the syscomments table of each data.
Run the following code to find the stored procedure using the tblProjects table in the TestDB1 and TestDB2 databases:

DECLARE @ txt varchar (100)
SELECT @ TXT = 'tblproject'
DECLARE @ SQL VARCHAR (MAX)
SELECT @ SQL = ISNULL (@ SQL + 'Union all', '') +
+ 'Select OBJ. Name, OBJ. Type ,'''
+ [Name]
+ '''As DBName from'
+ [Name]
+ '.. Sysobjects obj with (NOLOCK) INNER join'
+ [Name]
+ '.. Syscomments cmt with (NOLOCK) on obj. id = CMT. id WHERE (OBJ. type = 'P' or obj. type = ''v'') and cmt. text LIKE ''%'
+ @ TXT
+ '% '''
FROM Master. sys. SysDatabases WITH (NOLOCK)
WHERE Name IN ('testdb1', 'testdbdb ')
EXEC (@ SQL)

 

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.