-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-Views.sql
167 lines (141 loc) · 4.5 KB
/
3-Views.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--======================================= 3. Viw =======================================
/*
This file holds the query for copying the structure of views reference database to the destination database
To execute this, you need to replace "{Base}" with your reference database name
and replace "{Destination}" with your destination database name
This query creates a table called "#TempBase" in tempdb, so you need access to do that.
The query contains 2 parts which are "adding new Viwe" and "deleting current Views"
*/
--********** 3.1 Add Viwe
Begin /*AddViews*/
If Object_ID ('tempdb.dbo.#TempBase', 'U') Is Not Null Drop Table #TempBase;
If Object_ID ('tempdb.dbo.#TempDestination', 'U') Is Not Null Drop Table #TempDestination;
Select *
Into #TempBase
From {Base}.INFORMATION_SCHEMA.VIEWS
Select *
Into #TempDestination
From {Destination}.INFORMATION_SCHEMA.VIEWS
Select Distinct TABLE_NAME
From #TempBase
Where TABLE_NAME Not In (Select TABLE_NAME From #TempDestination)
Declare @tablenameAV nvarchar(50)
Declare @rowAV int
Set @rowAV = 0
Declare Cursor_AddViews Cursor For
Select Distinct TABLE_NAME
From #TempBase
Where TABLE_NAME Not In (Select TABLE_NAME From #TempDestination)
Open Cursor_AddViews
Fetch From Cursor_AddViews
Into @tablenameAV
While @@Fetch_STATUS=0
Begin
Declare @table_nameAV SYSNAME
Select @table_nameAV ='dbo.'+ @tablenameAV
Declare @AddViews NVARCHAR(MAX) = ''
Select @AddViews = c.VIEW_DEFINITION
From #TempBase as c
Where c.TABLE_NAME = @tablenameAV
--PRINT @AddViews
EXEC sys.sp_executesql @AddTable
Fetch Next From Cursor_AddViews
Into @tablenameAV
END
Close Cursor_AddViews
Deallocate Cursor_AddViews
End /*AddViews*/
--********** 3.2 Update Viwe
Begin /*UpdateViews*/
If Object_ID ('tempdb.dbo.#TempBase', 'U') Is Not Null Drop Table #TempBase;
If Object_ID ('tempdb.dbo.#TempDestination', 'U') Is Not Null Drop Table #TempDestination;
If Object_ID ('tempdb.dbo.#TempCursor', 'U') Is Not Null Drop Table #TempCursor;
Select c.*, v.VIEW_DEFINITION
Into #TempBase
From {Base}.INFORMATION_SCHEMA.VIEWS as v join
{Base}.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE as c on v.TABLE_NAME=c.VIEW_NAME
Select c.*,v.VIEW_DEFINITION
Into #TempDestination
From {Destination}.INFORMATION_SCHEMA.VIEWS as v join
{Destination}.INFORMATION_SCHEMA.VIEW_COLUMN_USAGE as c on v.TABLE_NAME=c.VIEW_NAME
SELECT * INTO #TempCursor FROM (
Select Distinct TABLE_NAME,COLUMN_NAME,VIEW_NAME
From #TempBase
Except
Select Distinct TABLE_NAME,COLUMN_NAME,VIEW_NAME
From #TempDestination
union
Select Distinct TABLE_NAME,COLUMN_NAME,VIEW_NAME
From #TempDestination
Except
Select Distinct TABLE_NAME,COLUMN_NAME,VIEW_NAME
From #TempBase)as tmp
Declare @ViewnameUV nvarchar(50)
Declare @TABLE_NAMEUV nvarchar(50)
Declare @COLUMN_NAMEUV nvarchar(50)
Declare @rowUV int
Set @rowUV = 0
Declare Cursor_UpdateViews Cursor For
select Distinct VIEW_NAME from #TempCursor
Open Cursor_UpdateViews
Fetch From Cursor_UpdateViews
Into @ViewnameUV
While @@Fetch_STATUS=0
Begin
Declare @View_nameUV SYSNAME
Select @View_nameUV ='dbo.'+ @ViewnameUV
Declare @UpdateViews NVARCHAR(MAX) = ''
Select @UpdateViews =
'DROP VIEW '+@View_nameUV
+' '+ c.VIEW_DEFINITION
From #TempBase as c
Where c.VIEW_NAME = @ViewnameUV
PRINT @UpdateViews
--EXEC sys.sp_executesql @AddTable
Fetch Next From Cursor_UpdateViews
Into @ViewnameUV
End
Close Cursor_UpdateViews
Deallocate Cursor_UpdateViews
End /*UpdateViews*/
--********** 3.3 Delete Viwe
Begin /*DeleteViews*/
If Object_ID ('tempdb.dbo.#TempBase', 'U') Is Not Null Drop Table #TempBase;
If Object_ID ('tempdb.dbo.#TempDestination', 'U') Is Not Null Drop Table #TempDestination;
Select *
Into #TempBase
From {Base}.INFORMATION_SCHEMA.VIEWS
Select *
Into #TempDestination
From {Destination}.INFORMATION_SCHEMA.VIEWS
Select Distinct TABLE_NAME
From #TempDestination
Except
Select Distinct TABLE_NAME
From #TempBase
Declare @TABLE_NAMEDV nvarchar(50)
Declare @rowDV int
Set @rowDV = 0
Declare Cursor_DeleteViews Cursor For
Select Distinct TABLE_NAME
From #TempDestination
Except
Select Distinct TABLE_NAME
From #TempBase
Open Cursor_DeleteViews
Fetch From Cursor_DeleteViews
Into @TABLE_NAMEDV
While @@Fetch_STATUS=0
Begin
Declare @View_nameDV SYSNAME
Select @View_nameDV ='dbo.'+ @TABLE_NAMEDV
Declare @DeleteViews NVARCHAR(MAX) = ''
Select @DeleteViews = 'DROP VIEW '+@View_nameDV
PRINT @DeleteViews
--EXEC sys.sp_executesql @AddTable
Fetch Next From Cursor_DeleteViews
Into @TABLE_NAMEDV
END
Close Cursor_DeleteViews
Deallocate Cursor_DeleteViews
End /*DeleteViews*/