Last active
May 7, 2025 16:11
-
-
Save ststeiger/892b496cc3295eaa9cae855ebe6c1460 to your computer and use it in GitHub Desktop.
Find the tables a VIEW depends on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
sch.name AS referenced_schema | |
,o.name AS referended_object | |
,o.type_desc AS referenced_object_type | |
-- ,pk_cols.COLUMN_NAME AS primary_key_column | |
FROM sys.sql_expression_dependencies AS d | |
JOIN sys.objects AS o ON d.referenced_id = o.object_id | |
JOIN sys.schemas AS sch ON sch.schema_id = o.schema_id | |
--LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS pk | |
-- ON pk.TABLE_NAME = o.name | |
-- AND pk.TABLE_SCHEMA = sch.name | |
-- AND pk.CONSTRAINT_TYPE = 'PRIMARY KEY' | |
--LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS pk_cols | |
-- ON pk.CONSTRAINT_NAME = pk_cols.CONSTRAINT_NAME | |
-- AND pk.TABLE_SCHEMA = pk_cols.TABLE_SCHEMA | |
-- AND pk.TABLE_NAME = pk_cols.TABLE_NAME | |
WHERE (1=1) | |
AND d.referencing_id = OBJECT_ID('dbo.V_COR_Objekte_Kategorien') | |
AND o.type IN ('U', 'V'); -- U = Table, V = View | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment