Skip to content

Commit 2e05916

Browse files
Added script to find tables with matching columns by name
1 parent 61a7b8e commit 2e05916

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
<documentation>
3+
<summary>Find all matching column names between tables. Useful for exploring databases with implied relationships but no foreign keys.</summary>
4+
<returns>1 data set: temp table #results.</returns>
5+
<issues>No</issues>
6+
<author>Tom Barrett</author>
7+
<created>2022-08-27</created>
8+
<modified>2022-08-27 by Tom Barrett</modified>
9+
<version>1.0</version>
10+
</documentation>
11+
*/
12+
13+
SET NOCOUNT ON;
14+
15+
/* create staging objects */
16+
IF object_id('tempdb..#tablesToQuery') IS NOT NULL
17+
BEGIN
18+
DROP TABLE #tablesToQuery
19+
END
20+
21+
CREATE TABLE #tablesToQuery (
22+
DatabaseName NVARCHAR(1000)
23+
, SchemaName NVARCHAR(1000)
24+
, TableName NVARCHAR(1000)
25+
, IsProcessed BIT DEFAULT 0
26+
)
27+
GO
28+
29+
IF object_id('tempdb..#results') IS NOT NULL
30+
BEGIN
31+
DROP TABLE #results
32+
END
33+
34+
CREATE TABLE #results (
35+
DatabaseName NVARCHAR(1000)
36+
, ColumnName NVARCHAR(1000)
37+
, Table1 NVARCHAR(1000)
38+
, Table2 NVARCHAR(1000)
39+
)
40+
GO
41+
42+
/* get list of tables */
43+
INSERT INTO #tablesToQuery (
44+
DatabaseName
45+
, SchemaName
46+
, TableName
47+
)
48+
SELECT TABLE_CATALOG
49+
, TABLE_SCHEMA
50+
, TABLE_NAME
51+
FROM INFORMATION_SCHEMA.TABLES
52+
ORDER BY TABLE_CATALOG
53+
, TABLE_SCHEMA
54+
, TABLE_NAME
55+
GO
56+
57+
/* loop through tables and get matching columns by name */
58+
DECLARE @x INT = 1;
59+
DECLARE @table NVARCHAR(1000);
60+
61+
WHILE @x <= (
62+
SELECT count('dracula')
63+
FROM #tablesToQuery
64+
WHERE IsProcessed = 0
65+
)
66+
BEGIN
67+
/* get next table*/
68+
SELECT TOP 1 @table = TableName
69+
FROM #tablesToQuery
70+
WHERE IsProcessed = 0;
71+
72+
/* insert into #results any tables + columns that match */
73+
WITH cte
74+
AS (
75+
SELECT TABLE_CATALOG
76+
, TABLE_SCHEMA
77+
, TABLE_NAME
78+
, COLUMN_NAME
79+
FROM INFORMATION_SCHEMA.COLUMNS
80+
)
81+
INSERT INTO #results
82+
SELECT cte1.TABLE_CATALOG
83+
, cte1.COLUMN_NAME
84+
, CONCAT (
85+
cte1.TABLE_SCHEMA
86+
, '.'
87+
, cte1.TABLE_NAME
88+
)
89+
, CONCAT (
90+
cte2.TABLE_SCHEMA
91+
, '.'
92+
, cte2.TABLE_NAME
93+
)
94+
FROM cte AS cte1
95+
JOIN cte AS cte2 ON cte1.TABLE_CATALOG = cte2.TABLE_CATALOG
96+
AND cte1.TABLE_SCHEMA collate Latin1_general_CI_AI = cte2.TABLE_SCHEMA collate Latin1_general_CI_AI /* remove this line to search different schemas*/
97+
AND cte1.TABLE_NAME collate Latin1_general_CI_AI <> cte2.TABLE_NAME collate Latin1_general_CI_AI
98+
AND cte1.COLUMN_NAME collate Latin1_general_CI_AI = cte2.COLUMN_NAME collate Latin1_general_CI_AI
99+
WHERE cte1.TABLE_NAME = @table
100+
/* add any other irrelevant or metadata columns to exclude */
101+
AND cte1.COLUMN_NAME collate Latin1_general_CI_AI NOT IN (
102+
'ID'
103+
, 'ACTIVE'
104+
, 'CREATEDBY'
105+
, 'ETC.'
106+
)
107+
108+
UPDATE #tablesToQuery
109+
SET IsProcessed = 1
110+
WHERE TableName = @table;
111+
END
112+
113+
/* return results */
114+
SELECT *
115+
FROM #results
116+
/* uncomment this line to search results for a specific table */
117+
/* WHERE Table1 = 'Schema.Table' */
118+
ORDER BY ColumnName
119+
, Table1
120+
, Table2
121+
GO
122+
123+
/* clean up */
124+
DROP TABLE #results
125+
GO
126+
127+
DROP TABLE #tablesToQuery
128+
GO
129+

0 commit comments

Comments
 (0)