Created
February 24, 2023 14:08
-
-
Save JesperJ/04e9e3f24d7d73dbd0dfd1a2bfe1fda8 to your computer and use it in GitHub Desktop.
MSSQL Data lineage from view
This file contains 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
DECLARE @view_name sysname = ''; | |
WITH cte AS ( | |
SELECT DISTINCT referenced_major_id | |
FROM sys.sql_dependencies | |
WHERE OBJECT_NAME(object_id) = @view_name | |
AND is_updated = 0 | |
) | |
SELECT o.name AS table_name, STRING_AGG(c.name, ', ') AS columns | |
FROM cte | |
JOIN sys.objects o ON o.object_id = cte.referenced_major_id AND o.type = 'U' | |
JOIN sys.columns c ON c.object_id = cte.referenced_major_id | |
GROUP BY o.name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment