I would like to avoid using sys.sql_dependencies because this feature will be removed in a future version of Microsoft SQL Server.
I also cannot use OBJECT_DEFINITION function because all my objects are encrypted.
So I came up with the following query which seems pretty simple and works for me so well that I even wrapped it to the function:
-- =============================================
-- Description: Gets all the stored procedures, functions and triggers referencing the specified column.
-- Example: SELECT * FROM dbo.UFN_GET_SP_FN_TR_REFERENCING_COLUMN(N'dbo', N'MY_TABLE', N'MY_COLUMN');
-- =============================================
CREATE FUNCTION dbo.UFN_GET_SP_FN_TR_REFERENCING_COLUMN
(
@SchemaName sysname,
@TableName sysname,
@ColumnName sysname
)
RETURNS TABLE
AS
RETURN
SELECT R.referencing_schema_name + N'.' + R.referencing_entity_name AS referencing_entity_name
FROM sys.dm_sql_referencing_entities(@SchemaName + N'.' + @TableName, 'OBJECT') AS R
INNER JOIN sys.objects AS O
ON R.referencing_id = O.object_id
WHERE O.[type] IN ('FN'/*SQL scalar function*/, 'IF'/*SQL inline table-valued function*/, 'TF'/*SQL table-valued-function*/, 'P'/*SQL Stored Procedure*/, 'TR' /*SQL DML trigger*/)
AND EXISTS(SELECT 1 FROM sys.dm_sql_referenced_entities (R.referencing_schema_name + N'.' + R.referencing_entity_name, 'OBJECT') AS RE WHERE RE.referenced_entity_name = @TableName AND RE.referenced_minor_name = @ColumnName);
GO