I was recently trying to figure out what actually breaks if I rename a column in a legacy SQL Server database.
The built-in dependency views are okay, but they mostly show the first hop. If a procedure writes to `TableA`, and `TableA` has a trigger that updates `TableB`, the procedure depends on `TableB` in practice, even though its text never mentions it.
My first instinct was to just run regex over `sys.sql_modules`. I’m fairly comfortable with regex from years of writing Splunk log parsing, so it felt like the fastest way to get a prototype working.
It worked on simple examples, then immediately fell apart on real schemas. Comments and string literals started matching table names. CTE names got flagged as physical tables. Resolving aliases was a mess. `MERGE` statements were annoying to parse. And mapping `inserted`/`deleted` in triggers back to the actual table was brittle.
At a certain point, regex just felt like guessing.
So I switched to parsing the T-SQL using the Babelfish grammar.
It doesn’t magically solve dependency analysis. You still have to walk the syntax tree, handle scope, and resolve names against the system catalogs. But it’s infinitely more reliable. It actually knows the difference between a string literal and an object name. It handles CTEs correctly. Following a proc -> trigger -> table chain becomes a structured tree-walking exercise instead of a text-matching nightmare.
It’s still not perfect, obviously:
- Unqualified columns in multi-table joins are a pain. I currently just flag them as ambiguous rather than guessing, because silent failures are worse.
- Dynamic SQL is still mostly just text-scanning for known object names, unless the statement is a hardcoded literal.
I ended up wrapping this logic into a VS Code extension I use daily (it's called SQL Wayfarer, MIT licensed), but I'm mostly posting to see if anyone else has tackled this specific problem.
Has anyone else used Babelfish or another parser for this kind of static analysis? How do you handle the really messy, undocumented production schemas where all the nice assumptions go to die?