-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGetTopicVersion.sql
130 lines (122 loc) · 4.4 KB
/
GetTopicVersion.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
--------------------------------------------------------------------------------------------------------------------------------
-- GET TOPIC VERSION
--------------------------------------------------------------------------------------------------------------------------------
-- Retrieves data associated with an individual topic version, as a means of either comparing or restoring a previous version.
--------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[GetTopicVersion]
@TopicID INT = -1,
@Version DATETIME2(7) = NULL
AS
--------------------------------------------------------------------------------------------------------------------------------
-- DECLARE AND DEFINE VARIABLES
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT KEY ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
SELECT TopicID,
ContentType,
ParentID,
TopicKey,
0 AS SortOrder
FROM Topics
WHERE TopicID = @TopicID
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT TOPIC ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicAttributes
AS (
SELECT TopicID,
AttributeKey,
AttributeValue,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID,
AttributeKey
ORDER BY Version DESC
)
FROM Attributes
WHERE TopicID = @TopicID
AND Version <= @Version
)
SELECT TopicID,
AttributeKey,
AttributeValue,
Version
FROM TopicAttributes
WHERE RowNumber = 1
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT EXTENDED ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicExtendedAttributes
AS (
SELECT TopicID,
AttributesXml,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY TopicID
ORDER BY Version DESC
)
FROM ExtendedAttributes
WHERE TopicID = @TopicID
AND Version <= @Version
)
SELECT TopicID,
AttributesXml,
Version
FROM TopicExtendedAttributes
WHERE RowNumber = 1
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT RELATIONSHIPS
--------------------------------------------------------------------------------------------------------------------------------
;WITH Relationships AS (
SELECT Source_TopicID,
RelationshipKey,
Target_TopicID,
IsDeleted,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
RelationshipKey
ORDER BY Version DESC
)
FROM [dbo].[Relationships]
WHERE Source_TopicID = @TopicID
AND Version <= @Version
)
SELECT Relationships.Source_TopicID,
Relationships.RelationshipKey,
Relationships.Target_TopicID,
Relationships.IsDeleted,
Relationships.Version
FROM Relationships
WHERE RowNumber = 1
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT REFERENCES
--------------------------------------------------------------------------------------------------------------------------------
;WITH TopicReferences AS (
SELECT Source_TopicID,
ReferenceKey,
Target_TopicID,
Version,
RowNumber = ROW_NUMBER() OVER (
PARTITION BY Source_TopicID,
ReferenceKey
ORDER BY Version DESC
)
FROM [dbo].[TopicReferences]
WHERE Source_TopicID = @TopicID
AND Version <= @Version
)
SELECT TopicReferences.Source_TopicID,
TopicReferences.ReferenceKey,
TopicReferences.Target_TopicID,
TopicReferences.Version
FROM TopicReferences
WHERE RowNumber = 1
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT HISTORY
--------------------------------------------------------------------------------------------------------------------------------
SELECT TopicID,
Version
FROM VersionHistoryIndex
WHERE TopicID = @TopicID