Skip to content
Advertisement

How to extract all tables (including where references) within a SQL query?

I’m working on a huge database, and I have to get a list of all tables used for 100+ procedures. So far I’ve been able to get a query that returns all the tables that the procedure is getting data from, but no all the tables that are referenced. For example:

Using that query if the procedure looks something like this:

I would only get table A, but I also need table B to show up. It seems that all the SQL queries around only gather data that is actually being integrated to the final select statements, but references like “where”, “in”, “not in”, coalesce, etc, are left aside. Maybe It’s kind of raw, but I just wondering how can I do to make sure that if a table is merely mentioned in any way I can get it. I haven’t been able to find anything, also tried with an python SQL parser, but with no results whatsoever.

Advertisement

Answer

here’s a more modern way:

a prettier approach:

Technically sys.objects is deprecated, so in the future YMMV.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement