Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


compare all tables and find number of common columns between all tables


compare all tables and find number of common columns between all tables

Author
Message
sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
is there any way to compare every table with each other in a database list the number of common column between each tables!! ?? prefer using join over subquery and information_schema.column, if possible thx.
example: lets say there are 4 tables in a database table1, table2, table3 and table4.
list1 list2 commonColumns
----- -------- --------------------
table1 table2 1
table1 table3 2
table1 table4 0
table2 table1 1
table2 table3 5
table2 table4 2
table3 table1 3
table3 table2 0
table3 table4 1
table4 table1 2
table4 table2 3
table4 table3 0

this is what i have got so far

select c1.table_name,c2.table_name,c1.COLUMN_NAME
from information_schema.columns c1
JOIN information_schema.columns c2
ON c1.column_name= c2.column_name
where c2.table_name <> c1.table_name
and c2.column_name = c1.column_name
ORDER BY c1.table_name, c2.TABLE_NAME
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2093 Visits: 22800


SELECT t1.name AS list1,
t2.name AS list2,
count(distinct c2.name) AS commonColumns
FROM sys.tables t1
CROSS JOIN sys.tables t2
INNER JOIN sys.columns c1 ON c1.object_id = t1.object_id
LEFT OUTER JOIN sys.columns c2 ON c2.object_id = t2.object_id AND c1.name=c2.name
GROUP BY t1.name,t2.name
ORDER BY t1.name,t2.name



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
thank you for your effort but I get an error when I run the query

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value ' Enrollments' to data type int.

how do I fix this conversion error?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
sqlsean (6/20/2013)
thank you for your effort but I get an error when I run the query

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value ' Enrollments' to data type int.

how do I fix this conversion error?



Actually Mark did a remarkable job posting a query with absolutely nothing to work with.

In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
sorry mark let me correct myself your query works jus fine and yes indeed it is a remarkable job for the information that I have provided. since you have already provided a working query let not bother about sample data.
however, when I try to get only records that has "commoncolumns" with value more than 1, then I get that conversion error.
how can I get through this? thank you again.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
sqlsean (6/20/2013)
sorry mark let me correct myself your query works jus fine and yes indeed it is a remarkable job for the information that I have provided. since you have already provided a working query let not bother about sample data.
however, when I try to get only records that has "commoncolumns" with value more than 1, then I get that conversion error.
how can I get through this? thank you again.



I only sort of looked at the post...given that we are dealing with common tables I agree that sample data would be rather silly in this case.

Can you simply add a having clause? This works for me at least.


SELECT t1.name AS list1,
t2.name AS list2,
count(distinct c2.name) AS commonColumns
FROM sys.tables t1
CROSS JOIN sys.tables t2
INNER JOIN sys.columns c1 ON c1.object_id = t1.object_id
LEFT OUTER JOIN sys.columns c2 ON c2.object_id = t2.object_id AND c1.name=c2.name
GROUP BY t1.name,t2.name
having count(distinct c2.name) > 1
ORDER BY t1.name,t2.name



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sql84
sql84
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 352
thank you sean, it worked.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16580 Visits: 17024
sqlsean (6/20/2013)
thank you sean, it worked.


You're welcome sqlme. :-D

Again my apologies for not reading the whole issue previously.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search