I have the following SQL query which is selecting all of the column names based on some search criteria, however; I want to get the database name as well. How would I do this?
I have not been able to accomplish this with a join as of now. I am not sure what the relationship is between sys.tables and sys.databases. Any guidance would be appreciated.
SELECT
c.name AS 'ColumnName',
t.name AS 'TableName'
FROM
sys.columns c
JOIN
sys.tables t ON c.object_id = t.object_id
WHERE
c.name LIKE '%Broker%'
ORDER BY
TableName, ColumnName;
Thank you. I was able to accomplish my goal by using the below code with your guidance. For further clarity I added the schema.
SELECT
'[' + SCHEMA_NAME(schema_id) + ']' + '.' + '[' + db_name() + ']' as "Database_Schema_Name",
t.name AS 'Table_Name',
c.name AS 'Column_Name'
FROM
sys.columns c
JOIN
sys.tables t ON c.object_id = t.object_id
WHERE
c.name LIKE '%Broker%'
ORDER BY
Table_Name, Column_Name