I would like to think of myself as a decent SQL Server programmer but this query always stumps me. I'm sure there is a much better way to query this.
SELECT DISTINCT
database_name,
CASE
WHEN ((SELECT MAX(1) FROM dbo.column_list WHERE column_list IS NOT NULL and database_id = d.database_id) = 1
OR (SELECT MAX(1) FROM dbo.table_list WHERE table_list IS NOT NULL and database_id = d.database_id) = 1)
THEN 1
ELSE 0
END AS 'has_definition'
FROM dbo.database_list d;
I have 3 tables database_list, table_list and a column_list. I'm trying to query for a given database in the database list to see if any of the underlying table or column has definitions in them. This query works now and returns a unique list of databases and a has_definition but this seems wrong to me.
Any thoughts would be helpful.