Sp_helptext returns the definition of the view (or SP). (I learned something new)
Let me explain what we want.
Suppose we have a database with a number of tables and views.
create table dbo.DaTable(
id int identity NOT NULL primary key
, a_code int NOT NULL
, amount int NOT NULL
, price decimal(8, 2) NOT NULL
, client_id int NOT NULL
)
GO
CREATE VIEW dbo.view2 AS
SELECT id as view_id, a_code as MyCode, amount MyAmount, price
FROM dbo.DaTable
WHERE a_code IN (6, 12)
GO
create table dbo.aggregatedDaTable(
id int identity NOT NULL primary key
, Filtered_code int NOT NULL
, ordered_amount int NOT NULL
, total_price decimal(8, 2) NOT NULL
)
GO
A SP could contain this code:
INSERT INTO dbo.aggregatedDaTable (Filtered_code, ordered_amount, total_price)
SELECT MyCode, SUM(MyAmount), SUM(MyAmount * price)
FROM dbo.view2
GROUP BY MyCode
We want to know in what tables and views data from dbo.DaTable is used.
We want to know where the data in dbo.aggregatedDaTable comes from.
If we can extract the dependencies between the tables/views and their columns, we can query that data set. We need the dependencies in a format somewhat like this:
Table_View Column SrcTable SourceColumn
dbo.aggregatedDaTable id null null
dbo.aggregatedDaTable Filtered_code dbo.view2 MyCode
dbo.aggregatedDaTable ordered_amount dbo.view2 MyAmount
dbo.aggregatedDaTable total_price dbo.view2 MyAmount
dbo.aggregatedDaTable total_price dbo.view2 price
dbo.view2 view_id dbo.DaTable id
dbo.view2 MyCode dbo.DaTable a_code
dbo.view2 MyAmount dbo.DaTable amount
dbo.view2 price dbo.DaTable price
If you want to know where the data in dbo.aggregatedDaTable.total_price comes from, you get dbo.view2.MyAmount and price.
If you want to know where the data in dbo.view2.MyAmount comes from, you get dbo.DaTable.Amount.
When someone finds an anomaly in dbo.aggregatedDaTable.total_price, you now have a means to find where the data originates from. It may be a bug in the view definition or a data quality problem in the staging table.
Suppose, you find out the data in the staging table dbo.DaTable is corrupted is some way, you can now find out all the tables and views that data is used in.