The obvious solution based on sysdepends - running alterproc for the SP with no changes before using sysdepends - works better than your proposed solution: it avoids listing tables that are mentioned only in comments.

Your solution suffers from a rather obvious defect: it doesn't handle views; that can be fixed rather trivially; avoiding comments is rather harder; handling dependencies arising from access to other databases or by the many indirect access mechanisms is quite hard too

If one really wants to understand dependencies between SPs and tables, one has to consider (i) tables in the current database accessed by the SP; (ii) tables in other databases accessed by the SP; (iii) tables (in this or other databases) involved in views accessed by this SP; (iv) tables (in this or other databases) used by SPs, or UDFs called by the SP; and (v) tables accessed (including indirect access through views or further triggers of SP or Function calls) by triggers fired by actions of this SP. Your current script is useful in a single database world with no triggers, views or functions and where no SP calls any other SPs, but not much use elsewhere.

Last idea: follow the rule of KISS (Keep It Simple & Stupid) write store procedure formatting it with the following rule: every command that reference a table should be on one line and should be the only one, e.g. UPDATE table1 ..SELECT a,b,cFROM dbo.table2CROSS JOIN server1.mydb.dbo.TABLE3FULL JOIN zzzzzDELETE FROM #kkkINSERT INTO somedb.dbo.tableX( A,b,c) select x,y,zFROM ANOTHERTABLE

So, your scanning routine should discard comments and searching only for DML. At last, replace commands with '', trim it and you’ll get the tablename + eventually alias for the table.

Here you are a sample:

create table #a (t varchar(8000))

delete #ainsert #a exec sp_helptext ‘mysp_elab’

update #a sett = rtrim(ltrim(replace(t,char(9),' ')))

-- write here code to cut out comments-- ...-- ...-- end

delete from #aWHERE t not like 'delete %' and t not like 'update %' and t not like 'insert %' and t not like 'from %' and t not like 'JOIN %'