I want these 5 columns as output when a stored procedure, in any database on the server, references a column in a specific database (let's say the database is AA).
Column_name Table_Name Schema_Name Procedure_Name Database_Name Four of the columns are really easy to get - for the current database that you're in:
SELECT TableName = t.Name, SchemaName = s.Name, ColumnName = c.Name, DatabaseName = DB_NAME() FROM sys.tables t INNER JOIN sys.schemas s ON [t].[schema_id] = [s].[schema_id] INNER JOIN sys.columns c ON [t].[object_id] = [c].[object_id] What you cannot get easily is all columns across all tables in all databases. Also: determining in which procedure each column is used is also rather tricky (or next to impossible).
sys.sysdepends table?From marc_s answer i generate a query which gives comma separated Procedure_Name depended by that table
SELECT TableName = t.Name, SchemaName = s.Name, ColumnName = c.Name, DatabaseName = DB_NAME(), STUFF((SELECT ',' + name FROM sys.procedures WHERE object_id in (SELECT distinct id from sys.sysdepends WHERE depid = (SELECT object_id FROM sys.tables WHERE name=t.Name) ) FOR XML PATH('')), 1, 0, '') AS sps FROM sys.tables t INNER JOIN sys.schemas s ON [t].[schema_id] = [s].[schema_id] INNER JOIN sys.columns c ON [t].[object_id] = [c].[object_id]
database..table AS tordatabase..[table] AS t1- now you have to parse all of the code for references to anyt.columnort1.columnand hope that they're prefixed (they're not always because people are lazy).