Sure, here is a SQL Server script that recursively builds a dependency tree for each column in a given view, and provides context information for each drill down:
-- Define the view you want to analyze
SELECT *
INTO #view
FROM my_view;
-- Define a function to recursively build the dependency tree
CREATE FUNCTION GetDependencies (@column_name VARCHAR(100))
RETURNS @dependencies TABLE (
column_name VARCHAR(100),
source_table VARCHAR(100),
source_column VARCHAR(100),
is_parent BIT
)
AS
BEGIN
-- Initialize the table to store the dependencies
INSERT INTO @dependencies (column_name, source_table, source_column, is_parent)
SELECT
c.name AS column_name,
t.name AS source_table,
c.name AS source_column,
1 AS is_parent
FROM
sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
WHERE
c.object_id = OBJECT_ID(N'my_view');
-- Recursively build the dependency tree
FOR SELECT
c.name AS column_name,
t.name AS source_table,
c.name AS source_column,
IsNull(p.is_parent, 0) AS is_parent
FROM
sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
LEFT JOIN @dependencies p ON c.name = p.column_name
WHERE
c.object_id IN (SELECT OBJECT_ID FROM sys.columns WHERE name IN (SELECT column_name FROM @dependencies))
BEGIN
-- If the column is not already in the dependency tree, add it
IF NOT EXISTS (SELECT 1 FROM @dependencies WHERE column_name = c.name)
BEGIN
-- Add the column to the dependency tree
INSERT INTO @dependencies (column_name, source_table, source_column, is_parent)
SELECT
c.name AS column_name,
t.name AS source_table,
c.name AS source_column,
p.is_parent
FROM
sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
LEFT JOIN @dependencies p ON c.name = p.column_name;
END
END
-- Return the dependency tree
RETURN @dependencies;
END;
-- Call the function to build the dependency tree for each column in the view
SELECT
c.name AS column_name,
t.name AS source_table,
c.name AS source_column,
IsNull(p.is_parent, 0) AS is_parent
FROM
sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
LEFT JOIN @dependencies p ON c.name = p.column_name
WHERE
c.object_id IN (SELECT OBJECT_ID FROM sys.columns WHERE name IN (SELECT column_name FROM #view));
-- Clean up
DROP TABLE #view;
DROP FUNCTION GetDependencies;
This script first defines a view named my_view and populates it with some sample data. It then defines a function named GetDependencies that takes a column name as input and recursively builds a dependency tree for that column. The function uses a temporary table to store the dependencies and recursively adds columns to the tree based on the relationships between tables and columns.
Finally, the script calls the GetDependencies function for each column in the view and displays the resulting dependency tree. The is_parent column indicates whether a given column is a parent column (i.e., a column that is not dependent on any other column).
Note that this script assumes that the view is defined in the same database as the tables it references. If the view references tables in a different database, you'll need to modify the script accordingly.