get a list of tables in SQL that contain certain column name

Hi Experts,

I want to enforce a one to many relationship in our database.

We have a table of units

And we have a couple of tables that has a column named units.

My goal is to enforce that:
1- Every unit has an entry in the units table
2- When I change a spelling of a unit in the units table, all related tables with that units should get changed as well.

Now I would like to get the following:

A) A list of tables containing the unit column.

B) A list of tables containing data in their unit column that does not have matching entry in the units table.

C) Or perhaps hear from you experts, how do you suggest I proceed in order to accomplish that?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

John TsioumprisSoftware & Systems EngineerCommented:
A list of tables containing the unit colum
Use NameOfYourDatabase
where COLUMN_NAME ='unit'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
In the end you would want to only store a unitid reference (foreign key) in any table referring to Units.
The steps you plan are soudn and will give you a hint on Units missing in the main Units table or being typos.

To find data without a match, do a left excluding join:
SELECT A.unit, Count(*) as howoften
FROM SomeTable A
ON A.unit = B.unit
GROUP By A.unit

It shouldn't be much work to make X copies and replace SomeTable with all tables names found through the Information Schema COLUMNS. Otherwise take a look at sp_msforeachtable and catch Errors on tables without unit column.

Bye, Olaf.
bfuchsAuthor Commented:
That seems to work.

I got dozens of tables that have this column, how exactly do I use that stored procedure, when I just execute that I get error message
"Msg 201, Level 16, State 4, Procedure sp_MSforeachtable, Line 0
Procedure or function 'sp_MSforeachtable' expects parameter '@command1', which was not supplied."

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Olaf DoschkeSoftware DeveloperCommented:
A simple usage of the stored proc would be like this:

create table #names (tablename varchar(50));

exec sp_msforeachtable 'insert into #names values (''?'')';

select * from #names;

Open in new window

Bye, Olaf.
Olaf DoschkeSoftware DeveloperCommented:
This would bring down all unit columns values:

create table #units (unit varchar(20));

exec sp_msforeachtable 'insert into #units select distinct [unit] from ?';

select distinct [unit] from #units;

Open in new window

It'll throw an error on each table not having a unit column, but continue processing.

Bye, Olaf.
bfuchsAuthor Commented:
Great Work, Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.