-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpdateTopic.sql
More file actions
103 lines (94 loc) · 4.02 KB
/
UpdateTopic.sql
File metadata and controls
103 lines (94 loc) · 4.02 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
--------------------------------------------------------------------------------------------------------------------------------
-- UPDATE TOPIC
--------------------------------------------------------------------------------------------------------------------------------
-- Used to update the attributes of a provided node
--------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[UpdateTopic]
@TopicID INT = -1 ,
@Attributes AttributeValues READONLY ,
@ExtendedAttributes XML = null ,
@Version DATETIME = null ,
@DeleteRelationships BIT = 0
AS
--------------------------------------------------------------------------------------------------------------------------------
-- SET DEFAULT VERSION DATETIME
--------------------------------------------------------------------------------------------------------------------------------
IF @Version IS NULL
SET @Version = GetDate()
--------------------------------------------------------------------------------------------------------------------------------
-- INSERT NEW ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
INSERT
INTO Attributes (
TopicID ,
AttributeKey ,
AttributeValue ,
Version
)
SELECT @TopicID,
AttributeKey,
AttributeValue,
@Version
FROM @Attributes
WHERE AttributeKey != 'ParentId'
AND IsNull(AttributeValue, '') != ''
--------------------------------------------------------------------------------------------------------------------------------
-- PULL PREVIOUS EXTENDED ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
DECLARE @PreviousExtendedAttributes XML
SELECT @PreviousExtendedAttributes = AttributesXml
FROM ExtendedAttributeIndex
WHERE TopicID = @TopicID
--------------------------------------------------------------------------------------------------------------------------------
-- ADD EXTENDED ATTRIBUTES, IF CHANGED
--------------------------------------------------------------------------------------------------------------------------------
IF CAST(@ExtendedAttributes AS NVARCHAR(MAX)) != CAST(@PreviousExtendedAttributes AS NVARCHAR(MAX))
BEGIN
INSERT
INTO ExtendedAttributes (
TopicID ,
AttributesXml ,
Version
)
VALUES (
@TopicID ,
@ExtendedAttributes ,
@Version
)
END
--------------------------------------------------------------------------------------------------------------------------------
-- INSERT NULL ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
INSERT INTO Attributes (
TopicID ,
AttributeKey ,
AttributeValue ,
Version
)
SELECT @TopicID,
AttributeKey,
'',
@Version
FROM @Attributes NullAttributes
WHERE IsNull(AttributeValue, '') = ''
AND (
SELECT TOP 1
AttributeValue
FROM Attributes
WHERE TopicID = @TopicID
AND AttributeKey = NullAttributes.AttributeKey
ORDER BY Version DESC
) != ''
--------------------------------------------------------------------------------------------------------------------------------
-- REMOVE EXISTING RELATIONS
--------------------------------------------------------------------------------------------------------------------------------
-- Relationships will be re-added by the Data Access Layer using Topics_PersistRelationships.
--------------------------------------------------------------------------------------------------------------------------------
DELETE
FROM [dbo].[Relationships]
WHERE Source_TopicID = @TopicID
AND @DeleteRelationships = 1
--------------------------------------------------------------------------------------------------------------------------------
-- RETURN TOPIC ID
--------------------------------------------------------------------------------------------------------------------------------
RETURN @TopicID