• I've since found another couple of issues. One is that it in the same way as I need and #endchars table to stop table1 matching on table12, I would also need a #startchars table to stop table1 matching on constable1. However if I include a startchars as another cross join like I do endchars, performance goes down the drain.

    For this reason, I worked on a modified version using patindex and a blacklist of endchars (rather than the whitelist above).

    Note that tables from other databases won't be considered (as we're looking at sysobjects from the current db.) Also any tables which are commented out will nonetheless be included.

    declare @sproc varchar(256)

    set @sproc = 'spToBeSearched'

    select so.name

    from sysobjects so,

    syscomments sc

    where so.type = 'U'

    and object_name(sc.id) = @sproc

    group by so.name

    having sum(patindex('%[^a-zA-Z0-9~]' + replace(so.name,'_','~') + '[^a-zA-Z0-9~]%', replace(sc.text,'_','~') + ' ')) <> 0

    order by name