-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetTopicID.sql
More file actions
45 lines (39 loc) · 1.96 KB
/
GetTopicID.sql
File metadata and controls
45 lines (39 loc) · 1.96 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
--------------------------------------------------------------------------------------------------------------------------------
-- GET TOPIC ID
--------------------------------------------------------------------------------------------------------------------------------
-- Given a particular topic key, finds the FIRST instance of the TopicID associated with that key. Be aware that since keys are
-- not guaranteed to be unique, this may yield unexpected results if multiple topics share the same key; in that case, the first
-- key in the hierarchy will be returned.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
FUNCTION [dbo].[GetTopicID] (
@TopicKey NVARCHAR(255)
)
RETURNS INT
AS
BEGIN
------------------------------------------------------------------------------------------------------------------------------
-- DECLARE AND DEFINE VARIABLES
------------------------------------------------------------------------------------------------------------------------------
DECLARE @TopicID INT
------------------------------------------------------------------------------------------------------------------------------
-- GET TOPIC ID BASED ON TOPIC KEY
------------------------------------------------------------------------------------------------------------------------------
SELECT TOP 1
@TopicID = Topics.TopicID
FROM Attributes Attributes
JOIN Topics Topics
ON Attributes.TopicID = Topics.TopicID
WHERE AttributeKey = 'Key'
AND AttributeValue = @TopicKey
ORDER BY RangeLeft DESC
OPTION (
OPTIMIZE
FOR ( @TopicKey = 'Root'
)
)
------------------------------------------------------------------------------------------------------------------------------
-- RETURN TOPIC ID
------------------------------------------------------------------------------------------------------------------------------
RETURN @TopicID
END