-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathViews.sql
More file actions
145 lines (113 loc) · 4.15 KB
/
Copy pathViews.sql
File metadata and controls
145 lines (113 loc) · 4.15 KB
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
IF EXISTS(SELECT 1 FROM sys.views WHERE name = 'vGetPersonTransactionStats')
DROP VIEW vGetPersonTransactionStats
GO
CREATE VIEW vGetPersonTransactionStats
AS
SELECT TOP (100) PERCENT PersonName, FORMAT(SUM(Amount),'C') TotalTransactions
,FORMAT(MAX(Amount),'C') MAXAmt
,FORMAT(AVG(Amount),'C') AVGAmt
,FORMAT(MIN(Amount),'C') MINAmt
,COUNT(*) Transactions
,DeptName FROM Departments D
JOIN Person P
ON D.DeptID = P.Department
JOIN Transactions T
ON P.PersonID = T.PersonID
GROUP BY PersonName,DeptName
ORDER BY SUM(Amount) DESC
GO
SELECT * FROM vGetPersonTransactionStats
--The Script From the View Can be found in the 'text' Column of the sys.comments table
-- Use the Following Query
SELECT OBJECT_NAME(object_id) AS ViewName,text AS ViewScript FROM sys.syscomments C
JOIN sys.views V
ON C.id = V.object_id
--OR
select OBJECT_DEFINITION(object_id('vGetPersonTransactionStats')) ViewScript
--OR
SELECT OBJECT_NAME(V.object_id) AS ViewName,definition AS ViewScript FROM sys.sql_modules M
JOIN sys.views V
ON M.object_id = V.object_id
-- To prevent this use WITH ENCRYPTION
GO
IF EXISTS(SELECT 1 FROM sys.views WHERE name = 'vGetDeptTotals')
DROP VIEW vGetDeptTotals
GO
CREATE VIEW vGetDeptTotals WITH ENCRYPTION
AS
SELECT TOP 100 Percent PersonName, FORMAT(SUM(Amount),'C') TotalTransactions
,FORMAT(MAX(Amount),'C') MAXAmt
,FORMAT(AVG(Amount),'C') AVGAmt
,FORMAT(MIN(Amount),'C') MINAmt
,COUNT(*) Transactions
,DeptName FROM Departments D
JOIN Person P
ON D.DeptID = P.Department
JOIN Transactions T
ON P.PersonID = T.PersonID
GROUP BY PersonName,DeptName
ORDER BY SUM(Amount) DESC
GO
SELECT OBJECT_NAME(object_id) AS ViewName,text AS ViewScript FROM sys.syscomments C
JOIN sys.views V
ON C.id = V.object_id
--OR
select OBJECT_DEFINITION(object_id('vGetPersonTransactionStats')) ViewScript
--OR
SELECT OBJECT_NAME(V.object_id) AS ViewName,definition AS ViewScript FROM sys.sql_modules M
JOIN sys.views V
ON M.object_id = V.object_id
--Script For Second View Appear as NULL or not at all
-------------------Update View ------------------
BEGIN TRAN
SELECT * FROM vGetPersonTransactionStats
--Can't change Views,Distinct,Group BY or Pivot/UnPivot with Aggregrates
UPDATE vGetPersonTransactionStats SET PersonName = 'Brent Lawrence' WHERE PersonName = 'Jack Ramthun'
SELECT * FROM vGetPersonTransactionStats
ROLLBACK TRAN
-----------------------------------------------
GO
IF EXISTS(SELECT 1 FROM sys.views WHERE name = 'vWTransactions')
DROP VIEW vWTransactions
GO
CREATE View vWTransactions
AS
SELECT P.PersonID,PersonName,DeptName,Amount,TransactionDate FROM Departments D
LEFT JOIN Person P
ON D.DeptID = P.Department
LEFT JOIN Transactions T
ON P.PersonID = T.PersonID
WHERE P.PersonID BETWEEN 5 AND 8
GO
SELECT * FROM vWTransactions
-----------------Update View ---------------
BEGIN TRAN
SELECT * FROM vWTransactions
--Change Name of Person 5
UPDATE vWTransactions SET PersonName = 'Brent Lawrence' WHERE PersonID = 5
SELECT * FROM vWTransactions
ROLLBACK TRAN
-- Adding WITH CHECK OPTION Prevents ANY DML take affect the Visibility of the Rows initally greated
-- E.G. UPDATE vWTransactions SET PersonID = 23 WHERE PersonID = 5
-- This is invalid because it removes all the ROWS with PersonID 5 from the View
-- On Views based on multiple tables DELETE cannot be used since it affects all of the tables
-- involved.
-- ------------------- Create Indexed View --------------------
--Schemas must be used
--Only inner joins
--Uses WITH SchemaBinding
IF EXISTS(SELECT 1 FROM sys.views WHERE name = 'vWTransactions')
DROP VIEW vWTransactions
GO
CREATE View dbo.vWTransactions
AS
SELECT P.PersonID,PersonName,DeptName,Amount,TransactionDate FROM dbo.Departments D
JOIN dbo.Person P
ON D.DeptID = P.Department
JOIN dbo.Transactions T
ON P.PersonID = T.PersonID
WHERE P.PersonID BETWEEN 5 AND 8
GO
--IF EXISTS(SELECT 1 FROM sys.indexes WHERE NAME = 'inx_vWTransactions')
-- SELECT 1
--CREATE UNIQUE CLUSTERED INDEX inx_vWTransactions on dbo.vWTransactions(PersonID,DeptName,Amount,TransactionDate)