Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Checking Table Relational
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 12/07/2012 :  03:41:19  Show Profile  Reply with Quote

Could Anyone Help me how to check a certain data on a table
if that data is related on another table?
Like for instance i have a 10 tables
and i have check one data on a table which has 5 connection related foreign key on the other tables

how to i check it on query sql server 2005/2008 if that data on
table1 is connected to the other tables i have..?

plz help sorry i'm new in database programming..


Flowing Fount of Yak Knowledge

United Kingdom
3385 Posts

Posted - 12/07/2012 :  03:52:02  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
Which way does the foreign key go?
I assume this is the master row and referenced by the other tables.

select t1.col, count(t2.col), count(t3.col), ...
from t1
left join t2 on t2.col = t1.col
left join t3 on t3.col = t1.col
--where t1.col = 'myval'

This will give a 0 for the table if it is not referenced.
Use the where clause if you want to check a specific row.
note: the referencing tables may not have an index on the column so this may table scan.

You can check the individual tables using an inner join rather than left join.

Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 12/07/2012 :  08:15:08  Show Profile  Reply with Quote
do you mean you've not set foreign keys already and need to find out which all table columns a column in a table is related to?

SQL Server MVP

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000