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?

Thanks
LVL 5
bfuchsAsked:
Who is Participating?
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
SELECT * from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME ='unit'

Open in new window

0

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
LEFT JOIN Units B
ON A.unit = B.unit
WHERE B.unit IS NULL
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.
0
bfuchsAuthor Commented:
@John
That seems to work.

@Olaf,
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."

Thanks,
Ben
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0
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.
0
bfuchsAuthor Commented:
Great Work, Thank you!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.