-1

I have a lot of tables inside some database of SQL Server and I think that some of those tables have some specific column that, I want to check if it exists and also see the values inside, if it actually exists.

I guess that the column was named like: ‘’Status’’

Please, consider that I don't have any idea about what are the values that maybe exist inside of this supposed column or even the kind of it.

Database name: PrincipalGroup

I won't say the name of the tables, because I don’t think it's feasible to write all the tables in this query, because there are many.

So, the point is: how can I query this by the easiest and simplest way?

  • 2
    Does this answer your question? [Find a string by searching all tables in SQL Server Management Studio 2008](https://stackoverflow.com/questions/15757263/find-a-string-by-searching-all-tables-in-sql-server-management-studio-2008) – SMor Jul 16 '21 at 12:57
  • You should be able to adapt this reply to your specific needs or find your own alternative with simple searching. I have to wonder why a column of the same name and datatype exists in many tables inside your database - removing / reducing the amount of duplicated information is a goal of proper normalization. Perhaps it is better to fix that problem if you can. – SMor Jul 16 '21 at 13:00

1 Answers1

0

You can try the following query that will produce a list of SQL statements you can then execute to "see the values inside" each table that contains the column named Status, and obviously tweak to your specific requirements.

select Concat('select distinct ', QUOTENAME(table_name, ''''), ' , [Status] from ', QuoteName(table_name))
from INFORMATION_SCHEMA.COLUMNS 
where COLUMN_NAME = 'Status'
Charlieface
  • 52,284
  • 6
  • 19
  • 43
Stu
  • 30,392
  • 6
  • 14
  • 33