snippetsqlMinor
How to find if a column is referenced in a computed column?
Viewed 0 times
computedreferencedhowcolumnfind
Problem
I'm trying to mass retype columns. That means first dropping and recreating any constraints they are part of.
I found columns referenced by these constraints
But I cannot find Computed columns.
I've looked into
There is also
Is there anywhere else I can look? If SQL Server can figure out the dependence, I thought I would be able to as well.
I found columns referenced by these constraints
- Foreign Keys,
- Primary Keys,
- Indexes,
- Check constraints,
- Rules,
- Default constraints.
But I cannot find Computed columns.
I've looked into
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE, but it doesn't include Computed Columns.There is also
sys.computed_columns which shows definition, but doesn't list columns in searchable manner.Is there anywhere else I can look? If SQL Server can figure out the dependence, I thought I would be able to as well.
Solution
Thanks to Scott Hodgin I found it in
sys.sql_expression_dependenciesSELECT
OBJECT_NAME(sed.referencing_id) AS referencingTable
, pc.[name] AS computedColumn
, pc.is_computed
, cc.[name] AS referencedcolumn
, cc.is_computed
FROM sys.sql_expression_dependencies sed
JOIN sys.[columns] pc ON sed.referencing_minor_id = pc.column_id AND sed.referencing_id = pc.[object_id]
JOIN sys.[columns] cc ON sed.referenced_minor_id = cc.column_id AND sed.referenced_id = cc.[object_id]
WHERE sed.referencing_minor_id > 0 -- referencing object is Column
AND sed.referenced_minor_id > 0 -- referenced object is Column
AND sed.referencing_id = sed.referenced_id -- references the same tableCode Snippets
SELECT
OBJECT_NAME(sed.referencing_id) AS referencingTable
, pc.[name] AS computedColumn
, pc.is_computed
, cc.[name] AS referencedcolumn
, cc.is_computed
FROM sys.sql_expression_dependencies sed
JOIN sys.[columns] pc ON sed.referencing_minor_id = pc.column_id AND sed.referencing_id = pc.[object_id]
JOIN sys.[columns] cc ON sed.referenced_minor_id = cc.column_id AND sed.referenced_id = cc.[object_id]
WHERE sed.referencing_minor_id > 0 -- referencing object is Column
AND sed.referenced_minor_id > 0 -- referenced object is Column
AND sed.referencing_id = sed.referenced_id -- references the same tableContext
StackExchange Database Administrators Q#236660, answer score: 7
Revisions (0)
No revisions yet.