I have many hundred SQL select statements stored in the database table in a nvarchar
column.
For each select statement, I need to find out what tables they read from. I need to do this programmatically (e.g. with T-SQL) and store the list of accessed tables in a database table.
I started doing this by calling the stored procedure sp_describe_first_result_set
. It works only partially.
For example:
EXEC sp_describe_first_result_set
@tsql = 'SELECT
a.code, b.customer_name
FROM table_a a
INNER JOIN table_b b ON a.code = b.code
WHERE NOT EXISTS (SELECT 1
FROM table_c c
WHERE a.code = c.code)',
@params = null,
@browse_information_mode = 2
This returns source_table
values table_a
and table_b
but not table_c
.
I need the list of accessed tables.
Any ideas on how I would achieve this?