SQL

Sql code posted
created at 23 Mar 19:38, updated at 29 Mar 18:40

Edit | Back
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Version 1.1
-- =============================================
-- Author:    Pavel Lastovka
-- Create date: 2007-07-05
-- Modification date: 2010-02-17 15:15
-- Description:  Update type of curves and variables
--        in local database from the server
-- =============================================
ALTER Procedure [object].[usp_update_export_types] 
  @lIdTypeProductionLine As UniqueIdentifier = Null,
  @lLoggingLevel As TinyInt = Null,
  @lUserId As UniqueIdentifier = Null Output
As
Begin
  Set NoCount On

  -- Default variables
  Declare @lProcName As NVarChar(128)
  Declare @lExecStr As NVarChar(MAX)
  Set @lProcName = N'[object].[usp_update_export_types]'
  
  -- Retrieve the user id
  If @lUserId Is Null
  Begin
    Begin Try
      Select Top 1
        @lUserId = [id_user]
      From
        [core].[user]
      Where
        [identifier] = N'system'
    End Try
    Begin Catch
      Print N'Exp: Cannot obtain a system user - ' + ERROR_MESSAGE()
      Return -1
    End Catch
    If @lUserId Is Null
    Begin
      Print N'Err: Cannot obtain a system user - the account doesn''t exist in the system'
      Return -1
    End
  End
  
  -- Obtain parameters
  If @lLoggingLevel Is Null
  Begin
    Begin Try
      Exec [core].[csp_get_param] @lName = N'LoggingLevel', @lGroupName = N'Local',
        @lDefaultValue = N'2', @lValue = @lExecStr Output, @lDataType = N'TinyInt',
        @lUserId = @lUserId
      Set @lLoggingLevel = Cast (@lExecStr As TinyInt)
    End Try
    Begin Catch
      Exec [core].[csp_write_to_log] @lLoggingLevel = 2, @lType = N'Exp', @lProcName = @lProcName,
        @lDescription = N'Cannot retrieve parameter LoggingLevel', @lUserId = @lUserId
      Set @lLoggingLevel = 2
    End Catch
  End
  
  -- Updating users
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating users', @lUserId = @lUserId
  
  Begin Try
    Delete From
      [core].[user]
    Where
      [id_user] Not In
      (
        Select
          [id_core_user]
        From
          [MIXCS].[dbo].[core_user]
      )
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot delete users', @lUserId = @lUserId
  End Catch
  
  Begin Try
    Update
      [core].[user]
    Set
      [identifier] = [mixcs_user].[identifier],
      [full_name] = [mixcs_user].[full_name],
      [password] = [mixcs_user].[password],
      [email] = [mixcs_user].[email],
      [x_user] = [mixcs_user].[x_user],
      [x_created] = [mixcs_user].[x_created],
      [x_modified] = GetUTCDate()
    From
      [core].[user] As [mixit_user]
    Inner Join
      [MIXCS].[dbo].[core_user] As [mixcs_user]
    On
      [mixit_user].[id_user] = [mixcs_user].[id_core_user]
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot update users', @lUserId = @lUserId
  End Catch
  
  Begin Try
    Insert Into
      [core].[user]
    (
      [id_user],
      [identifier],
      [full_name],
      [password],
      [email],
      [x_user],
      [x_created],
      [x_modified]
    )
    Select
      [id_core_user] As [id_user],
      [identifier] As [identifier],
      [full_name] As [full_name],
      [password] As [password],
      [email] As [email],
      [x_user] As [x_user],
      [x_created] As [x_created],
      GetUTCDate() As [x_modified]
    From
      [MIXCS].[dbo].[core_user]
    Where
      [id_core_user] Not In
      (
        Select
          [id_user]
        From
          [core].[user]
      )
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot insert a new user into the database', @lUserId = @lUserId
  End Catch
  
  Begin Try
    If Not Exists(Select [id_user] From [core].[user] Where [id_user] = @lUserId)
    Begin
      Select Top 1
        @lUserId = [id_user]
      From
        [core].[user]
      Where
        [identifier] = N'system'
    End
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot update an user id', @lUserId = @lUserId
  End Catch
  
  -- Update curve types
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating curve types', @lUserId = @lUserId
    
  Begin Try
    Set @lExecStr =
N'Delete From
  [type].[type_curve]
Where' +
  Case When
    @lIdTypeProductionLine Is Not Null
  Then
    N'
  [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  And '
  Else
    N'
'
  End +
    N'[id_type_curve] Not In
  (
    Select
      [id_type_curve]
    From
      [MIXCS].[dbo].[type_curve]' +
    Case When
      @lIdTypeProductionLine Is Not Null
    Then
      N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
    Else
      N''
    End +
      N'
  )'
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot delete curve types', @lUserId = @lUserId
  End Catch
  
  -- Update variable types
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating variable types', @lUserId = @lUserId
    
  Begin Try
    Set @lExecStr =
N'Delete From
  [type].[type_variable]
Where' +
  Case When
    @lIdTypeProductionLine Is Not Null
  Then
    N'
  [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  And '
  Else
    N'
'
  End +
N'[id_type_variable] Not In
  (
    Select
      [id_type_variable]
    From
      [MIXCS].[dbo].[type_variable]' +
    Case When
      @lIdTypeProductionLine Is Not Null
    Then
      N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
    Else
      N''
    End + N'
  )'
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot delete variable types', @lUserId = @lUserId
  End Catch
  
  -- Update production lines
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating production line types', @lUserId = @lUserId
    
  Begin Try
    Set @lExecStr = 
N'Delete From
  [type].[type_production_line]
Where
  [id_type_production_line] Not In
  (
    Select
      [id_type_production_line]
    From
      [MIXCS].[dbo].[type_production_line] ' +
      Case When
        @lIdTypeProductionLine Is Not Null
      Then
        N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  )
  And [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
      Else
        N'
  )'
      End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot delete production line types', @lUserId = @lUserId
  End Catch
  
  Begin Try
    Set @lExecStr =
N'Update
  [type].[type_production_line]
Set
  [identifier] = [mixcs_line].[identifier],
  [identification] = [mixcs_line].[identification],
  [x_user] = N''' + Cast(@lUserId As NVarChar(64)) + N''',
  [x_modified] = GetUTCDate()
From
  [type].[type_production_line] As [mixit_line]
Inner Join
  [MIXCS].[dbo].[type_production_line] As [mixcs_line]
On
  [mixcs_line].[id_type_production_line] = [mixit_line].[id_type_production_line]' +
  Case When
    @lIdTypeProductionLine Is Not Null
  Then
    N'
  And [mixcs_line].[id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
  Else
    N''
  End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot update production line types', @lUserId = @lUserId
  End Catch
  
  Begin Try
    Set @lExecStr =
N'Insert Into
  [type].[type_production_line]
(
  [id_type_production_line],
  [identifier],
  [identification],
  [x_user],
  [x_created]
)
Select
  [id_type_production_line] As [id_type_production_line],
  [identifier] As [identifier],
  [identification] As [identification],
  N''' + Cast(@lUserId As NVarChar(64)) + N''' As [x_user],
  [x_created] As [x_created]
From
  [MIXCS].[dbo].[type_production_line]
Where
  [id_type_production_line] Not In
  (
    Select
      [id_type_production_line]
    From
      [type].[type_production_line]' +
    Case When
      @lIdTypeProductionLine Is Not Null
    Then
      N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  )
  And [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
    Else
  N'
  )'
    End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot insert production line types', @lUserId = @lUserId
  End Catch

  -- Update curve types
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating curve types', @lUserId = @lUserId
    
  Begin Try
    Set @lExecStr =
N'Update
  [type].[type_curve]
Set
  [tag_id] = [mixcs_curve].[tag_index],
  [identifier] = [mixcs_curve].[identifier],
  [scale_factor] = [mixcs_curve].[scale_factor],
  [id_type_production_line] = [mixcs_curve].[id_type_production_line],
  [x_user] = N''' + Cast(@lUserId As NVarChar(64)) + N''',
  [x_modified] = GetUTCDate()
From
  [type].[type_curve] As [mixit_curve]
Inner Join
  [MIXCS].[dbo].[type_curve] As [mixcs_curve]
On
  [mixcs_curve].[id_type_curve] = [mixit_curve].[id_type_curve]' +
  Case When
    @lIdTypeProductionLine Is Not Null
  Then
    N'
  And [mixit_curve].[id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
  Else
    N''
  End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot update curve types', @lUserId = @lUserId
  End Catch

  Begin Try
    Set @lExecStr =
N'Insert Into
  [type].[type_curve]
(
  [id_type_curve],
  [id_type_production_line],
  [tag_id],
  [identifier],
  [scale_factor],
  [x_user],
  [x_created]
)
Select
  [id_type_curve] As [id_type_curve],
  [id_type_production_line] As [id_type_production_line],
  [tag_index] As [tag_id],
  [identifier] As [identifier],
  [scale_factor] As [scale_factor],
  N''' + Cast(@lUserId As NVarChar(64)) + N''' As [x_user],
  [x_created] As [x_created]
From
  [MIXCS].[dbo].[type_curve]
Where
  [id_type_curve] Not In
  (
    Select
      [id_type_curve]
    From
      [type].[type_curve]' +
    Case When
      @lIdTypeProductionLine Is Not Null
    Then
      N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  )
  And [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
    Else
  N'
  )'
    End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot insert curve types', @lUserId = @lUserId
  End Catch

  -- Update variable types
  Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Msg', @lProcName = @lProcName,
    @lDescription = N'Updating variable types', @lUserId = @lUserId
    
  Begin Try
    Set @lExecStr =
N'Update
  [type].[type_variable]
Set
  [tag_id] = [mixcs_variable].[tag_index],
  [identifier] = [mixcs_variable].[identifier],
  [scale_factor] = [mixcs_variable].[scale_factor],
  [id_type_production_line] = [mixcs_variable].[id_type_production_line],
  [coding_system] = [mixcs_variable].[group],
  [x_user] = N''' + Cast(@lUserId As NVarChar(64)) + N''',
  [x_modified] = GetUTCDate()
From
  [type].[type_variable] As [mixit_variable]
Inner Join
  [MIXCS].[dbo].[type_variable] As [mixcs_variable]
On
  [mixcs_variable].[id_type_variable] = [mixit_variable].[id_type_variable]' +
  Case When
    @lIdTypeProductionLine Is Not Null
  Then
    N'
  And [mixcs_variable].[id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
  Else
    N''
  End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot update variable types', @lUserId = @lUserId
  End Catch

  Begin Try
    Set @lExecStr =
N'Insert Into
  [type].[type_variable]
(
  [id_type_variable],
  [id_type_production_line],
  [tag_id],
  [identifier],
  [scale_factor],
  [coding_system],
  [x_user],
  [x_created]
)
Select
  [id_type_variable] As [id_type_variable],
  [id_type_production_line] As [id_type_production_line],
  [tag_index] As [tag_id],
  [identifier] As [identifier],
  [scale_factor] As [scale_factor],
  [group] As [coding_system],
  N''' + Cast(@lUserId As NVarChar(64)) + N''' As [x_user],
  [x_created] As [x_created]
From
  [MIXCS].[dbo].[type_variable]
Where
  [id_type_variable] Not In
  (
    Select
      [id_type_variable]
    From
      [type].[type_variable]' +
    Case When
      @lIdTypeProductionLine Is Not Null
    Then
      N'
    Where
      [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N'''
  )
  And [id_type_production_line] = N''' + Cast(@lIdTypeProductionLine As NVarChar(64)) + N''''
    Else
  N'
  )'
    End
    Exec(@lExecStr)
  End Try
  Begin Catch
    Exec [core].[csp_write_to_log] @lLoggingLevel = @lLoggingLevel, @lType = N'Exp', @lProcName = @lProcName,
      @lDescription = N'Cannot insert variable types', @lUserId = @lUserId
  End Catch

  Return 1
End
13.8 KB in 13 ms with coderay