Ad Code

How to find if Table is used in Stored Procedure in SQL Server

How to find if Table is used in Stored Procedure in SQL Server

 You will receive a list of the tables and views that the system stored process and view used. The stored procedure or view name that you supply as a parameter will return the tables and views that are used by the object.

One drawback of using sp depends is that it won't display tables or views that aren't in the current database. This information may be inaccurate if you have used objects from other databases.


--By using sp_depends, You can provide Stored Procedure Name or View name as parameter


EXEC sp_depends '[dbo].[TableName]'



Second Method:

--Get Information from sys.sysdepends 


SELECT DISTINCT OBJECT_NAME(SD.id) AS StoredProcedureNameName,
 OB.name AS TableOrViewName
 FROM sys.sysdepends SDINNER JOIN sys.sysobjects O 
ON SD.id=O.id
 INNER JOIN sys.sysobjects OB 
ON SD.depid=OB.id 
AND O.xtype='P'


Third Method:

--Use sys.all_sql_modules system view to get information if table is used
--in a Stored Procedure
 
SELECT OBJECT_NAME(OBJECT_ID),definition 
 FROM sys.all_sql_modules 
WHERE definition LIKE '%vw_Employee%'

 

 

Post a Comment

0 Comments

Close Menu