forked from lyhabc/SQLServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindTableWithThreeColumns.sql
118 lines (92 loc) · 3.03 KB
/
FindTableWithThreeColumns.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
-- Replace
/* SET Database Name */
USE OurDatabaseName
GO
/* Two Columns */
DECLARE @Results TABLE (
RTable VARCHAR(100),
RColumn VARCHAR(100)
)
DECLARE @column1 AS VARCHAR(50)
DECLARE @column2 AS VARCHAR(50)
/* ONLY MAKE CHANGES HERE: */
SET @column1 = 'Our Column Name'
SET @column2 = 'Our Column Name'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", ISC.COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column1 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column2 +'%'
SELECT DISTINCT RTable, COUNT(RColumn) AS "Having 2 Columns"
FROM @results
GROUP BY RTable
HAVING COUNT(RColumn) > 1
/* Three Columns */
USE OurDatabaseName
GO
DECLARE @Results TABLE (
RTable VARCHAR(100),
RColumn VARCHAR(100)
)
DECLARE @column1 AS VARCHAR(50)
DECLARE @column2 AS VARCHAR(50)
DECLARE @column3 AS VARCHAR(50)
/* ONLY MAKE CHANGES HERE: */
SET @column1 = 'Our Column Name'
SET @column2 = 'Our Column Name'
SET @column3 = 'Our Column Name'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", ISC.COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column1 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column2 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column3 +'%'
SELECT DISTINCT RTable, COUNT(RColumn) AS "Having 3 Columns"
FROM @results
GROUP BY RTable
HAVING COUNT(RColumn) > 2
/* Four Columns */
USE OurDatabaseName
GO
DECLARE @Results TABLE (
RTable VARCHAR(100),
RColumn VARCHAR(100)
)
DECLARE @column1 AS VARCHAR(50)
DECLARE @column2 AS VARCHAR(50)
DECLARE @column3 AS VARCHAR(50)
DECLARE @column4 AS VARCHAR(50)
/* ONLY MAKE CHANGES HERE: */
SET @column1 = 'Our Column Name'
SET @column2 = 'Our Column Name'
SET @column3 = 'Our Column Name'
SET @column4 = 'Our Column Name'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", ISC.COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column1 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column2 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column3 +'%'
INSERT INTO @Results
SELECT ISC.TABLE_NAME AS "Table", COLUMN_NAME AS "Full Column Name"
FROM information_schema.COLUMNS ISC
WHERE ISC.column_name LIKE '%' + @column4 +'%'
SELECT DISTINCT RTable, COUNT(RColumn) AS "Having 4 Columns"
FROM @results
GROUP BY RTable
HAVING COUNT(RColumn) > 3