-
Notifications
You must be signed in to change notification settings - Fork 10
/
stored-procedure-GetDimensionSetID.sql
155 lines (127 loc) · 4.23 KB
/
stored-procedure-GetDimensionSetID.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
USE DynamicsNAV
GO
IF OBJECT_ID('Cronus$GetDimensionSetID') IS NOT NULL
DROP PROCEDURE [dbo].[Cronus$GetDimensionSetID]
GO
CREATE PROCEDURE [dbo].[Cronus$GetDimensionSetID]
(
@DimensionSetID INT OUTPUT,
@Debug BIT = 1
)
AS
BEGIN
SET NOCOUNT ON
IF @Debug = 1 PRINT(' -> GetDimensionSetID call')
IF 1=0 /* for the sake of intellisense */
CREATE TABLE #DimensionSetEntry(
DimCode nvarchar(50) Collate Latin1_General_100_CI_AS NOT NULL,
DimValueCode nvarchar(50) Collate Latin1_General_100_CI_AS NOT NULL,
DimValueID int NOT NUll
)
DECLARE @DimSetEntry TABLE
(
DimCode nvarchar(50) Collate Latin1_General_100_CI_AS NOT NULL,
DimValueCode nvarchar(50) Collate Latin1_General_100_CI_AS NOT NULL,
DimValueID int NOT NUll
)
INSERT INTO @DimSetEntry (DimCode, DimValueCode, DimValueID)
SELECT DimCode, DimValueCode, DimValueID
FROM #DimensionSetEntry
WHERE DimCode <> '' AND DimValueCode <> ''
DECLARE @DimValueID INT,
@Found BIT,
@InUse BIT,
@DimSetID INT,
@ParentDimensionSetID INT
DECLARE DimCursor CURSOR FOR
SELECT DimValueID
FROM @DimSetEntry
ORDER BY DimValueID
SET @Found = 1
SET @DimensionSetID = 0
OPEN DimCursor
FETCH NEXT FROM DimCursor INTO @DimValueID
WHILE(@@FETCH_STATUS = 0)
BEGIN
IF ISNULL(@DimValueID,0) = 0
BEGIN
RAISERROR(N'Dimension Value ID cannot be blank.', 16, 1)
RETURN
END;
IF @Found = 1
BEGIN
SELECT @DimSetID = 0, @InUse = 0
SELECT @ParentDimensionSetID = [Parent Dimension Set ID], @DimSetID = [Dimension Set ID], @InUse = [In Use]
FROM [Cronus$Dimension Set Tree Node]
WHERE [Parent Dimension Set ID] = @DimensionSetID AND [Dimension Value ID] = @DimValueID
IF @DimSetID <> 0
SET @DimensionSetID = @DimSetID
ELSE
SET @Found = 0
END
IF @Found = 0
BEGIN
INSERT INTO [Cronus$Dimension Set Tree Node] ([Parent Dimension Set ID], [Dimension Value ID], [In Use])
VALUES(@DimensionSetID, @DimValueID, 0)
SELECT @ParentDimensionSetID = [Parent Dimension Set ID], @DimensionSetID = [Dimension Set ID], @InUse = [In Use]
FROM [Cronus$Dimension Set Tree Node]
WHERE [Parent Dimension Set ID] = @DimensionSetID
AND [Dimension Value ID] = @DimValueID
END
IF @Debug = 1
PRINT(CAST(@DimensionSetID AS NVARCHAR(MAX)) + ' - DimeValueID: ' + CAST(@DimValueID AS NVARCHAR(MAX)) + ' - In Use: ' + CAST(@InUse AS NVARCHAR(MAX)))
FETCH NEXT FROM DimCursor INTO @DimValueID
END
CLOSE DimCursor
DEALLOCATE DimCursor
IF ISNULL(@DimensionSetID,0) = 0
BEGIN
RAISERROR(N'Dimension Set ID was not found.', 16, 1)
RETURN
END
IF @InUse = 0
BEGIN
IF @Debug = 1
PRINT 'Inserting DImension Set ' + CAST(@DimensionSetID AS NVARCHAR(MAX))
UPDATE [Cronus$Dimension Set Tree Node]
SET [In Use] = 1
WHERE [Parent Dimension Set ID] = @ParentDimensionSetID AND [Dimension Value ID] = @DimValueID
INSERT INTO [Cronus$Dimension Set Entry]
([Dimension Set ID], [Dimension Code], [Dimension Value Code], [Dimension Value ID])
SELECT @DimensionSetID, DimCode, DimValueCode, DimValueID
FROM @DimSetEntry
END
IF @Debug = 1 PRINT(' -- GetDimensionSetID finished')
/*
Found := TRUE;
DimSetTreeNode."Dimension Set ID" := 0;
REPEAT
DimSetEntry.TESTFIELD("Dimension Value ID");
IF Found THEN
IF NOT DimSetTreeNode.GET(DimSetTreeNode."Dimension Set ID",DimSetEntry."Dimension Value ID") THEN BEGIN
Found := FALSE;
DimSetTreeNode.LOCKTABLE;
END;
IF NOT Found THEN BEGIN
DimSetTreeNode."Parent Dimension Set ID" := DimSetTreeNode."Dimension Set ID";
DimSetTreeNode."Dimension Value ID" := DimSetEntry."Dimension Value ID";
DimSetTreeNode."Dimension Set ID" := 0;
DimSetTreeNode."In Use" := FALSE;
IF NOT DimSetTreeNode.INSERT(TRUE) THEN
DimSetTreeNode.GET(DimSetTreeNode."Parent Dimension Set ID",DimSetTreeNode."Dimension Value ID");
END;
UNTIL DimSetEntry.NEXT = 0;
IF NOT DimSetTreeNode."In Use" THEN BEGIN
IF Found THEN BEGIN
DimSetTreeNode.LOCKTABLE;
DimSetTreeNode.GET(DimSetTreeNode."Parent Dimension Set ID",DimSetTreeNode."Dimension Value ID");
END;
DimSetTreeNode."In Use" := TRUE;
DimSetTreeNode.MODIFY;
InsertDimSetEntries(DimSetEntry,DimSetTreeNode."Dimension Set ID");
END;
DimSetEntry.COPY(DimSetEntry2);
EXIT(DimSetTreeNode."Dimension Set ID");
*/
END
GO