-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled-1.sql
303 lines (290 loc) · 10.1 KB
/
Untitled-1.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
if (exists (select *
from sys.objects
where object_id = OBJECT_ID(N'tsp_CreateVwVisa')
and type in (N'P', N'PC')))
begin
drop procedure [dbo].[tsp_CreateVwVisa];
end;
go
create procedure [dbo].[tsp_CreateVwVisa]
as
begin
declare @vSqlQuery NVARCHAR(MAX)
declare @vVisaQuery NVARCHAR(MAX)
declare @vVisaSchemaCount INT = 0
declare @vTsiName NVARCHAR(100)
declare @vTsiOwnerEntityColumn NVARCHAR(100)
declare @vTsiNameEntityColumn NVARCHAR(100)
declare @vTsiStatusEntityColumn NVARCHAR(100)
declare @vTsiSysModuleId uniqueidentifier
declare @vTsiVisaName NVARCHAR(100)
IF OBJECT_ID(N'TsiVwVisa', N'V') IS NOT NULL
BEGIN
DROP VIEW [dbo].[TsiVwVisa]
END;
set @vSqlQuery = 'CREATE VIEW [dbo].[TsiVwVisa] AS';
DECLARE VisaSchema CURSOR local FOR
SELECT
sesr.[ColumnName] AS TsiOwnerEntityColumn,
(SELECT s.name
FROM sys.columns s
INNER JOIN sys.types t ON s.system_type_id=t.user_type_id and t.is_user_defined=0
INNER JOIN sys.objects o ON s.object_id=o.object_id
INNER JOIN sys.schemas sh on o.schema_id=sh.schema_id
join SysSchema ss on ss.Name = o.name and cast(MetaData as varchar(max)) like '%MetaData.Schema.D4%'
WHERE o.type_desc = 'USER_TABLE' and
o.name IN
(
SELECT
[Name]
FROM
[SysSchema]
WHERE
[Name] = smv.[TsiName]
)
and exists( select null from fn_listextendedproperty('TS.EntitySchemaColumn.UID', 'schema', 'dbo', 'table', o.name, 'column', s.name) where substring(cast(value as varchar(38)),2,36)
= SUBSTRING(cast(ss.MetaData as varchar(max)),CHARINDEX('MetaData.Schema.D4',cast(ss.MetaData as varchar(max)))+20,36))) AS TsiNameEntityColumn,
smv.[TsiStatusEntityColumn],
ss.[Name] as TsiVisaName,
sm.[Code] as TsiName,
sm.[Id] as TsiSysModuleId
FROM
[SysModuleVisa] AS smv
INNER JOIN
[SysSchema] ss
ON ss.[UId] = smv.[VisaSchemaUId]
INNER JOIN
[SysModule] sm
ON sm.[SysModuleVisaId] = smv.[Id]
LEFT JOIN [SysEntitySchemaReference] sesr
ON sesr.[Id] =
(
SELECT TOP 1
[Id]
FROM
[SysEntitySchemaReference]
WHERE
[ColumnUId] = smv.TsiInitiatorColumnUId AND SysSchemaId
IN
(
SELECT
[Id]
FROM
[SysSchema]
WHERE
[Name] = smv.[TsiName]
)
)
WHERE smv.[TsiIsActive] = 1 AND ss.[Name] <> 'TsiTender'
OPEN VisaSchema
FETCH NEXT FROM VisaSchema
into @vTsiOwnerEntityColumn,@vTsiNameEntityColumn,@vTsiStatusEntityColumn,@vTsiVisaName,@vTsiName,@vTsiSysModuleId
WHILE @@FETCH_STATUS = 0
BEGIN
IF @vVisaSchemaCount > 0
SET @vSqlQuery = @vSqlQuery + CHAR(10) + ' UNION ALL ';
ELSE
SET @vSqlQuery = @vSqlQuery + CHAR(10) + 'with CTE1 AS (
select main.*, ss.Name as [TsiEntitySchemaName] from (';
SET @vVisaQuery = 'SELECT
[v].[Id] [Id],
[v].[CreatedOn] [CreatedOn],
[v].[CreatedById] [CreatedById],
[v].[ModifiedOn] [ModifiedOn],
[v].[ModifiedById] [ModifiedById],
[v].[ProcessListeners] [ProcessListeners],
[v].[Objective] [Objective],
[v].[VisaOwnerId] [VisaOwnerId],
[v].[IsAllowedToDelegate] [IsAllowedToDelegate],
[v].[DelegatedFromId] [DelegatedFromId],
[v].[StatusId] [StatusId],
[v].[SetById] [SetById],
[v].[SetDate] [SetDate],
[v].[IsCanceled] [IsCanceled],
[v].[Comment] [Comment],
[v].[IsRequiredDigitalSignature] [IsRequiredDigitalSignature],
[v].[TsiPosition] [TsiPosition],
[v].[TsiApprovalFlowTypeId] [TsiApprovalFlowTypeId],
[v].[TsiDurationInHours] [TsiDurationInHours],
[v].[TsiTaskCreatedOn] [TsiTaskCreatedOn],
[v].[TsiTakenToWorkOn] [TsiTakenToWorkOn],
[v].[TsiPlanTaskCompletionOn] [TsiPlanTaskCompletionOn],
[v].[TsiIsTaskExecutionControled] [TsiIsTaskExecutionControled],
[v].[TsiCycle] [TsiCycle],
[v].[TsiIsNotify] [TsiIsNotify],
[v].[TsiIsNotifyByEmail] [TsiIsNotifyByEmail],
[v].[TsiRejectHandlerId] [TsiRejectHandlerId],
[v].[TsiEscalationHandlerId] [TsiEscalationHandlerId],
[v].[TsiUseProcessReject] [TsiUseProcessReject],
[v].[TsiGroupNumber] [TsiGroupNumber],
[v].[TsiIsCurrentGroup] [TsiIsCurrentGroup],
[v].[TsiReasonForCancellationId] [TsiReasonForCancellationId],
[v].[TsiIsStartNewGroup] [TsiIsStartNewGroup],
[v].[TsiIsReminded] [TsiIsReminded],
[v].[TsiIsEscalated] [TsiIsEscalated],
[v].[TsiVisaTypeId] [TsiVisaTypeId],
[v].[TsiPerform] [TsiPerform],
[v].[TsiIsIndependent] [TsiIsIndependent],
[v].[TsiTimeSpent] [TsiTimeSpent],
CONVERT(uniqueidentifier, '''+CAST(@vTsiSysModuleId as nvarchar(38))+''') [TsiSysModuleId],
[o].[Id] [TsiRecordId],
[o].['+@vTsiOwnerEntityColumn+'Id] [TsiInitiatorId],
[o].['+@vTsiNameEntityColumn+'] [TsiName],
[o].['+@vTsiStatusEntityColumn+'Id] [TsiDocStateId]
FROM
['+@vTsiVisaName+'] [v] WITH(NOLOCK)
INNER JOIN
['+@vTsiName+'] [o] WITH(NOLOCK)
ON ([v].['+REPLACE(@vTsiVisaName,'Visa','Id')+'] = [o].[Id])
INNER JOIN
[VisaStatus] [vs] WITH(NOLOCK)
ON ([vs].[Id] = [v].[StatusId]
AND [vs].[IsFinal] = 0)
WHERE
[TsiIsCurrentGroup] = 1'
SET @vSqlQuery = @vSqlQuery + @vVisaQuery;
SET @vVisaSchemaCount = @vVisaSchemaCount + 1;
FETCH NEXT FROM VisaSchema
INTO @vTsiOwnerEntityColumn,@vTsiNameEntityColumn,@vTsiStatusEntityColumn,@vTsiVisaName,@vTsiName,@vTsiSysModuleId
END
CLOSE VisaSchema;
DEALLOCATE VisaSchema;
SET @vSqlQuery = @vSqlQuery + CHAR(10) + ' UNION ALL ';
SET @vVisaQuery = 'SELECT
[v].[Id] [Id],
[v].[CreatedOn] [CreatedOn],
[v].[CreatedById] [CreatedById],
[v].[ModifiedOn] [ModifiedOn],
[v].[ModifiedById] [ModifiedById],
[v].[ProcessListeners] [ProcessListeners],
[v].[Objective] [Objective],
[sau].[Id] [VisaOwnerId],
[v].[IsAllowedToDelegate] [IsAllowedToDelegate],
[v].[DelegatedFromId] [DelegatedFromId],
[v].[TsiStateId] [StatusId],
[v].[SetById] [SetById],
[v].[TsiSetDate] [SetDate],
[v].[IsCanceled] [IsCanceled],
[v].[Comment] [Comment],
[v].[IsRequiredDigitalSignature] [IsRequiredDigitalSignature],
[v].[TsiPosition] [TsiPosition],
[v].[TsiApprovalFlowTypeId] [TsiApprovalFlowTypeId],
[v].[TsiDurationInHours] [TsiDurationInHours],
[v].[TsiTaskCreatedOn] [TsiTaskCreatedOn],
[v].[ModifiedOn] [TsiTakenToWorkOn],
[v].[TsiPlanTaskCompletionOn] [TsiPlanTaskCompletionOn],
[v].[TsiIsTaskExecutionControled] [TsiIsTaskExecutionControled],
[v].[TsiCycle] [TsiCycle],
[v].[TsiIsNotify] [TsiIsNotify],
[v].[TsiIsNotifyByEmail] [TsiIsNotifyByEmail],
[v].[TsiRejectHandlerId] [TsiRejectHandlerId],
[v].[TsiEscalationHandlerId] [TsiEscalationHandlerId],
[v].[TsiUseProcessReject] [TsiUseProcessReject],
[v].[TsiGroupNumber] [TsiGroupNumber],
[v].[TsiIsCurrentGroup] [TsiIsCurrentGroup],
[v].[TsiReasonForCancellationId] [TsiReasonForCancellationId],
[v].[TsiIsStartNewGroup] [TsiIsStartNewGroup],
[v].[TsiIsReminded] [TsiIsReminded],
[v].[TsiIsEscalated] [TsiIsEscalated],
[v].[TsiVisaTypeId] [TsiVisaTypeId],
[v].[TsiPerform] [TsiPerform],
[v].[TsiIsIndependent] [TsiIsIndependent],
[v].[TsiTimeSpent] [TsiTimeSpent],
CONVERT(uniqueidentifier, ''DD3E8C6E-C29F-478D-A65B-2F543F0BA39E'') [TsiSysModuleId],
[o].[Id] [TsiRecordId],
[v].[CreatedById] [TsiInitiatorId],
[o].[UsrName] [TsiName],
NULL [TsiDocStateId],
N''UsrAreas'' [TsiEntitySchemaName]
FROM
[TsiTender] [v] WITH(NOLOCK)
INNER JOIN
[UsrAreas] [o] WITH(NOLOCK)
ON ([v].[TsiAreaId] = [o].[Id])
INNER JOIN
[TsiAreaTender] [tat] WITH(NOLOCK)
ON ([v].[TsiAreaTenderId] = [tat].[Id]
AND
[tat].[TsiIsActive] = 1)
INNER JOIN
[SysAdminUnit] [sau] WITH(NOLOCK)
ON ([v].[TsiOwnerId] = [sau].[ContactId])'
SET @vSqlQuery = @vSqlQuery + @vVisaQuery;
IF @vVisaSchemaCount > 0
set @vSqlQuery = @vSqlQuery + ') as main
INNER JOIN
[SysModule] [sm] WITH(NOLOCK)
ON main.[TsiSysModuleId] = [sm].[Id]
INNER JOIN
[SysModuleEntity] [sme] WITH(NOLOCK)
ON [sm].[SysModuleEntityId] = [sme].[Id]
INNER JOIN
[SysSchema] [ss] WITH(NOLOCK)
ON [sme].[SysEntitySchemaUId] = [ss].[UId]
),
CTE2 AS
(
SELECT
[q].*
FROM
(
SELECT
[Id],
ROW_NUMBER() OVER (PARTITION BY VisaOwnerId, TsiRecordId ORDER BY TsiPosition DESC) [TsiPositionOrder]
FROM
[cte1] WITH(NOLOCK)) AS [q]
WHERE
[q].[TsiPositionOrder] = 1
)
SELECT
[c1].*
FROM
[CTE1] [c1] WITH(NOLOCK)
INNER JOIN
[CTE2] [c2] WITH(NOLOCK) ON ([c1].[Id] = [c2].[Id]);';
ELSE
SET @vSqlQuery = @vSqlQuery + CHAR(10) + 'SELECT NULL AS Id,
NULL AS [CreatedOn],
NULL AS [CreatedById],
NULL AS [ModifiedOn],
NULL AS [ModifiedById],
NULL AS [ProcessListeners],
NULL AS [TsiInitiatorId],
NULL AS [TsiStatusId],
NULL AS [TsiTakenToWorkOn],
NULL AS [TsiSetDate],
NULL AS [TsiName],
NULL AS [TsiSysModuleId],
NULL AS [TsiVisaOwnerId],
NULL AS [TsiRecordId],
NULL AS [TsiPosition],
NULL AS [TsiDocStateId],
NULL AS [TsiTaskCreatedOn],
NULL AS [TsiDurationInHours],
NULL AS [TsiPlanTaskCompletionOn],
NULL AS [BnzUseProcessReject],
NULL AS [BnzRejectHandlerId],
NULL AS [BnzReasonForCancellationId],
NULL AS [BnzIsTaskExecutionControled],
NULL AS [BnzIsStartNewGroup],
NULL AS [BnzIsReminded],
NULL AS [BnzIsNotifyByEmail],
NULL AS [BnzIsNotify],
NULL AS [BnzIsEscalated],
NULL AS [BnzIsCurrentGroup],
NULL AS [BnzGroupNumber],
NULL AS [BnzEscalationHandlerId],
NULL AS [BnzCycle],
NULL AS [BnzApprovalFlowTypeId],
NULL AS [BnzSetById],
NULL AS [BnzObjective],
NULL AS [BnzIsRequiredDigitalSignature],
NULL AS [BnzIsCanceled],
NULL AS [BnzIsAllowedToDelegate],
NULL AS [BnzDelegatedFromId],
NULL AS [TsiExecDuration],
NULL AS [TsiExceedingDuration],
NULL AS [TsiEntitySchemaName];';
EXECUTE sp_executesql @vSqlQuery;
end;
GO