转:获取一个表内的依赖的所有存储过程
使用方法如下:
/*===============================================================
Example : EXEC
dbo.usp_GetDependantObjects
@varTableName = 'sysdiagrams', @varSPName= NULL
===============================================================*/
存储过程的脚本如下:
ALTER PROCEDURE
[dbo].[usp_GetDependantObjects]
(
@varTableName VARCHAR(100) = NULL,
@varSPName VARCHAR(255) = NULL
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @varSQL VARCHAR(8000)
SET @varSQL = 'SELECT DISTINCT sysO.id, sysO.name as Procedure_Name, '
+ ' sysO2.name as Table_Name, sysD.depid, '
+ ' sysD.depnumber '
+ 'FROM sysdepends sysD'
+ ' INNER JOIN sysobjects sysO '
+ ' ON sysO.id = sysD.id '
+ CASE WHEN @varSPName IS NULL THEN '' ELSE ' AND sysO.name = ''' + @varSPName + ''' ' END
+ ' INNER JOIN sysobjects sysO2 '
+ ' ON sysO2.id = sysD.depid '
+ ' AND sysD.depnumber = 1 '
+ CASE WHEN @varTableName IS NULL THEN '' ELSE ' AND sysO2.name = ''' + @varTableName +
''' ' END
+ ' ORDER BY sysO.name, sysO2.name '
--PRINT @varSQL
EXECUTE ( @varSQL )
END