0

I want to count tables that have a specific column name. For instance, dbo.Management has 300 tables. Some tables have ProtectionKey column, and others don't.

I need a list of all the tables that have that column ProtectionKey, or at least a count of them. I have looked into similar examples but those are either counting ALL columns in a DB or ALL columns in one table.

2 Answers 2

2

I would personally use the sys objects for this, as INFORMATION_SCHEMA can return incorrect information:

SELECT s.[name] AS SchemaName,
       t.[name] AS TableName
FROM sys.schemas s
     JOIN sys.tables t ON s.schema_id = t.schema_id
     JOIN sys.columns c ON t.object_id = c.object_id
WHERE c.[name] = N'ProtectionKey';
8
  • I am not so advanced, what fields should be replaced to match the name of my DB?
    – HomeMade
    Commented Feb 20, 2020 at 12:39
  • 1
    None, @HomeMade , you run the query with a connection to your database.
    – Thom A
    Commented Feb 20, 2020 at 12:40
  • Hmm, I add `` use Management`` before that query and I get zero.
    – HomeMade
    Commented Feb 20, 2020 at 12:42
  • 1
    Or the account running the script doesn't have permissions to see them Commented Feb 20, 2020 at 12:43
  • 1
    Good point, @MartinSmith . I assumed the OP was running on a "privileged" account. ;)
    – Thom A
    Commented Feb 20, 2020 at 12:44
2

Use INFORMATION_SCHEMA.COLUMNS:

select c.*
from INFORMATION_SCHEMA.COLUMNS c
where column_name = 'ProtectionKey';

This has many columns. The ones you want are TABLE_NAME and TABLE_SCHEMA.

Also, this only works in one database at a time, so you need to run it in each database where you want to search for the tables.

8
  • TABLE_SCHEMA can be wrong, From the Docs: "** Important ** Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of a object is to query the sys.objects catalog view."
    – Thom A
    Commented Feb 20, 2020 at 12:34
  • 2
    @Larnu - If you do EXEC sp_helptext 'INFORMATION_SCHEMA.COLUMNS' you see that TABLE_SCHEMA is derived from applying SCHEMA_NAME to schema_id from sys.objects anyway so not sure if that warning is out dated Commented Feb 20, 2020 at 12:37
  • 1
    I've asked the question, in case you're interested in the answer, @MartinSmith . Seems that in September 2019 it was still a "problem", but they gave no information as to why. The fact that is uses sys.objects underneath, and SCHEMA_NAME() in the VIEW suggests to me that the problem is therefore SCHEMA_NAME(); but that's the first I've heard.
    – Thom A
    Commented Feb 20, 2020 at 16:06
  • 1
    @Larnu thanks. There is no similar warning in the docs for SCHEMA_NAME() so if that is the issue it should be mentioned there too Commented Feb 20, 2020 at 16:12
  • 1
    Agreed, @MartinSmith , and why I mentioned it in the GitHub report. Hopefully this one won't get glossed over as a "it's still bad, close please" no response on why.
    – Thom A
    Commented Feb 20, 2020 at 16:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.