If you have to deal with linked servers, I adapted @MilicaMedic's answer to work with cross-server dependencies. I also output column names where they are available depending on.
You can use it like this:
create table #dependencies ( referencing_server nvarchar(128), referencing_database nvarchar(128), referencing_schema nvarchar(128), referencing_object_name nvarchar(128), referencing_column nvarchar(128), referenced_server nvarchar(128), referenced_database nvarchar(128), referenced_schema nvarchar(128), referenced_object_name nvarchar(128), referenced_column nvarchar(128) ); insert @dependencies exec crossServerDependencies 'ThisServerName, LinkedServerName, LinkedServerName2, etc'
From there, you attach it to your AllObjects table, as you described in your answer.
My code requires two external functions: "splitString" and "AddBracketsWhenNeeded". You can simplify the first and completely eliminate the second as you wish. But I use them for other things so that they go into my implementation. The code for both is at the bottom.
Here is the basic procedure:
create procedure crossServerDependencies @server_names_csv nvarchar(500) = null -- csv list of server names you want to pull dependencies for as -- Create output table if object_id('tempdb..#dependencies') is not null drop table
Code for AddBracketsWeNeeded:
create function AddBracketsWhenNecessary ( @objectName nvarchar(250) ) returns nvarchar(250) as begin if left(@objectName, 1) = '[' and right(@objectName, 1) = ']' return @objectName; declare @hasInvalidCharacter bit; select @hasInvalidCharacter = max(isInvalid) from dbo.splitString(@objectName, null) chars cross apply (select isLetter = patindex('[az,_]', val), isNumber = PATINDEX('[0-9]', val) ) getCharType cross apply (select isInvalid = case when isLetter = 1 then 0 when isNumber = 1 and not chars.id = 1 then 0 else 1 end ) getValidity return case when @hasInvalidCharacter = 1 then '[' else '' end + @objectName + case when @hasInvalidCharacter = 1 then ']' else '' end; end
Finally, my splitter function (but see Arnold Fribble here if you want a simpler version, or use the built-in function if you have SqlServer 2016 or higher):
create function splitString ( @stringToSplit nvarchar(max), @delimiter nvarchar(50) ) returns table as return with split_by_delimiter as ( select id = 1, start = 1, stop = convert(int, charindex(@delimiter, @stringToSplit) ) union all select id = id + 1, start = newStart, stop = convert(int, charindex(@delimiter, @stringToSplit, newStart) ) from split_by_delimiter cross apply (select newStart = stop + len(@delimiter)) ap where Stop > 0 ), split_into_characters as ( select id = 1, chr = left(@stringToSplit,1) union all select id = id + 1, chr = substring(@stringToSplit, ID + 1, 1) from split_into_characters where id < len(@stringToSplit) ) select id, val = ltrim(rtrim(substring( @stringToSplit, start, case when stop > 0 then stop - start else len(@stringtosplit) end ))) from split_by_delimiter where len(@delimiter) > 0 union all select id, val = chr from split_into_characters where @delimiter = '' or @delimiter is null
I had to make small changes to the real code that I use, so if there are any link errors, please let me know in the comments and I will edit.