Including one more script to my script volt.
I have always wanted to easily identify the dependent
objects for a procedure. The core of code was grabbed from http://www.sqlservercentral.com/scripts/Cross+Reference/108582/,
I have changed the original script to accommodate the common filter conditions
and other dependent objects that I
thought was needed.
The query identifies the following dependent objects
- - Tables
- - Views
- - Functions
- - Procedures
Parameter -- @ProcedureName ( by default it will be set to @ProcedureName = '-1')
-- @ProcedureName = '-1' will
-- generate dependencies for all proceudre in the currnt database
-- @ProcedureName = '' + '%' -- will
populate the dependencies which are in the LIKE clause
*/
DECLARE
@ProcedureName
sysname
SET @ProcedureName = '-1'
-- @ProcedureName = '' + '%' -- Enable
only if needed
SELECT SOP.name
SP_SchemaName ,
OBJECT_NAME(S.object_id) as StoredPorcedure_Name ,
OBJ_TYPE Dependent_ObjectType ,
TableSchemaName
Dependent_Object_ShchemaName,
TableName Dependent_Object_Name
FROM sys.sql_modules S
JOIN sys.objects SO ON S.object_id = SO.oBJECT_id
JOIN sys.SCHEMAS SOP ON SO.Schema_ID =SOP.Schema_ID
Join
(
SELECT
SO.Name TableSchemaName ,
T.Name
TableName ,
CASE
WHEN
T.Type = 'U' THEN 'TABLE'
WHEN
T.Type = 'V' THEN 'VIEW'
WHEN
T.Type = 'FN' THEN 'SCALAR FUNCTION'
WHEN
T.Type = 'P' THEN 'PRCEDURE'
ELSE
T.Type
END
OBJ_TYPE
FROM
sys.objects T
JOIN
sys.SCHEMAS SO ON T.Schema_ID = SO.Schema_ID
WHERE
T.type in ( 'U' ,'v','FN','P')
) A on 1=1
WHERE objectproperty(S.object_id,'IsProcedure') = 1
AND CHARINDEX(A.TableName,Definition,0)<>0
AND
(@ProcedureName = '-1' OR OBJECT_NAME(S.object_id) LIKE @ProcedureName
)
AND A.TableName <> OBJECT_NAME(S.object_id) -- THIS IS TO AVOID THE
SAME PROCEDURE NAME APPEARING AS A DEPENDENT
ORDER BY
OBJECT_NAME(S.object_id) -- ordered on Procedure
Name
Comments
Post a Comment