-
Notifications
You must be signed in to change notification settings - Fork 1
/
asset5-scripts.sql
6803 lines (6140 loc) · 400 KB
/
asset5-scripts.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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create database Asset5
go
use Asset5
go
--IF NOT EXISTS (SELECT * FROM sys.triggers WHERE parent_class_desc = 'DATABASE' AND name = N'ctrd_DDL_PROCEDURE_EVENTS_vb')
--EXECUTE dbo.sp_executesql N'
--CREATE TRIGGER [ctrd_DDL_PROCEDURE_EVENTS_vb] ON DATABASE WITH EXECUTE AS CALLER
-- FOR CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION, CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE AS
-- EXTERNAL NAME [VbTriggers].[VbTriggers.Triggers].[trigger_DDL_PROCEDURE_EVENTS]'
--
--GO
--ENABLE TRIGGER [ctrd_DDL_PROCEDURE_EVENTS_vb] ON DATABASE
--GO
--EXEC sys.sp_addextendedproperty @name=N'AutoDeployed', @value=N'yes' , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_PROCEDURE_EVENTS_vb'
--
--GO
--EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFile', @value=N'Triggers.vb' , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_PROCEDURE_EVENTS_vb'
--
--GO
--EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFileLine', @value=17 , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_PROCEDURE_EVENTS_vb'
--GO
--IF NOT EXISTS (SELECT * FROM sys.triggers WHERE parent_class_desc = 'DATABASE' AND name = N'ctrd_DDL_TABLE_EVENTS ')
--EXECUTE dbo.sp_executesql N'
--CREATE TRIGGER [ctrd_DDL_TABLE_EVENTS ] ON DATABASE WITH EXECUTE AS CALLER
-- FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE AS
-- EXTERNAL NAME [CSrpTrigger].[Triggers].[ddl_table]'
--
--GO
--ENABLE TRIGGER [ctrd_DDL_TABLE_EVENTS ] ON DATABASE
--GO
--EXEC sys.sp_addextendedproperty @name=N'AutoDeployed', @value=N'yes' , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_TABLE_EVENTS '
--
--GO
--EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFile', @value=N'Trigger1.cs' , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_TABLE_EVENTS '
--
--GO
--EXEC sys.sp_addextendedproperty @name=N'SqlAssemblyFileLine', @value=92 , @level0type=N'TRIGGER', @level0name=N'ctrd_DDL_TABLE_EVENTS '
--
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'rolUser' AND type = 'R')
CREATE ROLE [rolUser] AUTHORIZATION [dbo]
GO
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'util')
EXEC sys.sp_executesql N'CREATE SCHEMA [util] AUTHORIZATION dbo'
GO
IF NOT EXISTS (SELECT * FROM sys.synonyms WHERE name = N'Eq')
CREATE SYNONYM [dbo].[Eq] FOR [dbo].[Equipment]
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'typPhone' AND ss.name = N'dbo')
CREATE TYPE [dbo].[typPhone] FROM [varchar](25) NOT NULL
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'typEmail' AND ss.name = N'dbo')
CREATE TYPE [dbo].[typEmail] FROM [varchar](255) NOT NULL
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[fnQuarterString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'Create Function [util].[fnQuarterString]
-- returns quarter in form of ''3Q2000''.
(
@dtmDate datetime
)
Returns char(6) -- quarter like 3Q2000
As
Begin
Return (DateName(q, @dtmDate) + ''Q'' + DateName(yyyy, @dtmDate))
End
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[fnThreeBusDays]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'Create Function [util].[fnThreeBusDays]
-- returns date 3 business day after the specified date
(@dtmDate datetime)
Returns datetime
As
Begin
Declare @inyDayOfWeek tinyint
Set @inyDayOfWeek = DatePart(dw, @dtmDate)
Set @dtmDate = Convert(datetime, Convert(varchar, @dtmDate, 101))
If @inyDayOfWeek = 1 -- Sunday
Return DateAdd(d, 3, @dtmDate )
If @inyDayOfWeek = 7 -- Saturday
Return DateAdd(d, 4, @dtmDate )
If @inyDayOfWeek = 6 -- Friday
Return DateAdd(d, 5, @dtmDate )
If @inyDayOfWeek = 5 -- Thursday
Return DateAdd(d, 5, @dtmDate )
If @inyDayOfWeek = 4 -- Wednesday
Return DateAdd(d, 5, @dtmDate )
Return DateAdd(d, 3, @dtmDate )
End
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fnDueDays]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'Create Function [dbo].[fnDueDays]
-- return list of due days for the leasing
(
@dtsStartDate smalldatetime,
@dtsEndDate smalldatetime,
@chvLeaseFrequency varchar(20)
)
Returns @tblTerms table
(
TermID int,
DueDate smalldatetime
)
As
Begin
Declare @insTermsCount smallint -- number of intervals
Declare @insTerms smallint -- number of intervals
-- calculate number of terms
Select @insTermsCount =
Case @chvLeaseFrequency
When ''monthly''
then DateDIFF(month, @dtsStartDate, @dtsEndDate)
When ''semi-monthly''
then 2 * DateDIFF(month, @dtsStartDate, @dtsEndDate)
When ''bi-weekly''
then DateDIFF(week, @dtsStartDate, @dtsEndDate)/2
When ''weekly''
then DateDIFF(week, @dtsStartDate, @dtsEndDate)
When ''quarterly''
then DateDIFF(qq, @dtsStartDate, @dtsEndDate)
When ''yearly''
then DateDIFF(y, @dtsStartDate, @dtsEndDate)
End
-- generate list of due dates
Set @insTerms = 1
While @insTerms <= @insTermsCount
Begin
Insert @tblTerms (TermID, DueDate)
Values (@insTerms, Convert(smalldatetime, CASE
When @chvLeaseFrequency = ''monthly''
then DateADD(month,@insTerms, @dtsStartDate)
When @chvLeaseFrequency = ''semi-monthly''
and @insTerms/2 = Cast(@insTerms as float)/2
then DateADD(month, @insTerms/2, @dtsStartDate)
When @chvLeaseFrequency = ''semi-monthly''
and @insTerms/2 <> Cast(@insTerms as float)/2
then DateADD(dd, 15,
DateADD(month, @insTerms/2, @dtsStartDate))
When @chvLeaseFrequency = ''bi-weekly''
then DateADD(week, @insTerms*2, @dtsStartDate)
When @chvLeaseFrequency = ''weekly''
then DateADD(week, @insTerms, @dtsStartDate)
When @chvLeaseFrequency = ''quarterly''
then DateADD(qq, @insTerms, @dtsStartDate)
When @chvLeaseFrequency = ''yearly''
then DateADD(y, @insTerms, @dtsStartDate)
End , 105))
Select @insTerms = @insTerms + 1
End
Return
End
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_Terms_List]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE Procedure [dbo].[ap_Terms_List]
-- return list of due days for the leasing
@dtsStartDate smalldatetime,
@dtsEndDate smalldatetime,
@chvLeaseFrequency varchar(20)
As
set nocount on
declare @insDueDates smallint -- number of intervals
-- calculate number of DueDates
select @insDueDates =
Case @chvLeaseFrequency
When ''monthly''
then DateDiff(month, @dtsStartDate, @dtsEndDate)
When ''semi-monthly''
then 2 * DateDiff(month, @dtsStartDate, @dtsEndDate)
When ''bi-weekly''
then DateDiff(week, @dtsStartDate, @dtsEndDate)/2
When ''weekly''
then DateDiff(week, @dtsStartDate, @dtsEndDate)
When ''quarterly''
then DateDiff(qq, @dtsStartDate, @dtsEndDate)
When ''yearly''
then DateDiff(y, @dtsStartDate, @dtsEndDate)
END
-- generate list of due dates using temporary table
Create table #DueDates (ID int)
while @insDueDates >= 0
begin
insert #DueDates (ID)
values (@insDueDates)
select @insDueDates = @insDueDates - 1
end
-- display list of Due dates
select ID+1, Convert(varchar,
Case
When @chvLeaseFrequency = ''monthly''
then DateAdd(month,ID, @dtsStartDate)
When @chvLeaseFrequency = ''semi-monthly''
and ID/2 = CAST(ID as float)/2
then DateAdd(month, ID/2, @dtsStartDate)
When @chvLeaseFrequency = ''semi-monthly''
and ID/2 <> CAST(ID as float)/2
then DateAdd(dd, 15,
DateAdd(month, ID/2, @dtsStartDate))
When @chvLeaseFrequency = ''bi-weekly''
then DateAdd(week, ID*2, @dtsStartDate)
When @chvLeaseFrequency = ''weekly''
then DateAdd(week, ID, @dtsStartDate)
When @chvLeaseFrequency = ''quarterly''
then DateAdd(qq, ID, @dtsStartDate)
When @chvLeaseFrequency = ''yearly''
then DateAdd(y, ID, @dtsStartDate)
END , 105) [Due date]
from #DueDates
order by ID
-- wash the dishes
drop table #DueDates
return
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[InventorySum]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[InventorySum](
[ID] [int] NULL,
[InventoryId] [int] NULL,
[Make] [varchar](50) NULL,
[Model] [varchar](50) NULL,
[Location] [varchar](50) NULL,
[FirstName] [varchar](30) NULL,
[LastName] [varchar](30) NULL,
[AcquisitionType] [varchar](12) NULL,
[Address] [varchar](50) NULL,
[City] [varchar](50) NULL,
[ProvinceId] [char](3) NULL,
[Country] [varchar](50) NULL,
[EqType] [varchar](50) NULL,
[Phone] [varchar](20) NULL,
[Fax] [varchar](20) NULL,
[Email] [varchar](128) NULL,
[UserName] [varchar](50) NULL,
[MakeModelSIdx] [int] NULL,
[LFNameSIdx] [int] NULL,
[CountrySIdx] [int] NULL
) ON [PRIMARY]
END
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_upgraddiagrams]') AND type in (N'P', N'PC'))
--BEGIN
--EXEC dbo.sp_executesql @statement = N'
-- CREATE PROCEDURE [dbo].[sp_upgraddiagrams]
-- AS
-- BEGIN
-- IF OBJECT_ID(N''dbo.sysdiagrams'') IS NOT NULL
-- return 0;
--
-- CREATE TABLE dbo.sysdiagrams
-- (
-- name sysname NOT NULL,
-- principal_id int NOT NULL, -- we may change it to varbinary(85)
-- diagram_id int PRIMARY KEY IDENTITY,
-- version int,
--
-- definition varbinary(max)
-- CONSTRAINT UK_principal_name UNIQUE
-- (
-- principal_id,
-- name
-- )
-- );
--
--
-- /* Add this if we need to have some form of extended properties for diagrams */
-- /*
-- IF OBJECT_ID(N''dbo.sysdiagram_properties'') IS NULL
-- BEGIN
-- CREATE TABLE dbo.sysdiagram_properties
-- (
-- diagram_id int,
-- name sysname,
-- value varbinary(max) NOT NULL
-- )
-- END
-- */
--
-- IF OBJECT_ID(N''dbo.dtproperties'') IS NOT NULL
-- begin
-- insert into dbo.sysdiagrams
-- (
-- [name],
-- [principal_id],
-- [version],
-- [definition]
-- )
-- select
-- convert(sysname, dgnm.[uvalue]),
-- DATABASE_PRINCIPAL_ID(N''dbo''), -- will change to the sid of sa
-- 0, -- zero for old format, dgdef.[version],
-- dgdef.[lvalue]
-- from dbo.[dtproperties] dgnm
-- inner join dbo.[dtproperties] dggd on dggd.[property] = ''DtgSchemaGUID'' and dggd.[objectid] = dgnm.[objectid]
-- inner join dbo.[dtproperties] dgdef on dgdef.[property] = ''DtgSchemaDATA'' and dgdef.[objectid] = dgnm.[objectid]
--
-- where dgnm.[property] = ''DtgSchemaNAME'' and dggd.[uvalue] like N''_EA3E6268-D998-11CE-9454-00AA00A3F36E_''
-- return 2;
-- end
-- return 1;
-- END
-- '
--END
--GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_InvSum_Generate]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ap_InvSum_Generate]
@debug [int] = 0
WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON
SET XACT_ABORT ON
declare @intTransactionCountOnEntry int
create table #Inv(ID int identity(1,1),
Inventoryid int,
Make varchar(50),
Model varchar(50),
Location varchar(50),
Status varchar(15),
FirstName varchar(30),
LastName varchar(30),
AcquisitionType varchar(12),
Address varchar(50),
City varchar(50),
ProvinceId char(3),
Country varchar(50),
EqType varchar(50),
Phone varchar(20),
Fax varchar(20),
Email varchar(128),
UserName varchar(50),
MakeModelSIdx int,
LFNameSIdx int,
CountrySIdx int)
-- get result set
insert into #Inv(InventoryId, Make, Model,
Location, FirstName, LastName,
AcquisitionType, Address,
City, ProvinceId, Country,
EqType,Phone, Fax,
Email, UserName
)
/*SELECT Inventory.Inventoryid, Equipment.Make, Equipment.Model,
Location.Location, Contact.FirstName,
Contact.LastName, AcquisitionType.AcquisitionType, Location.Address,
Location.City, Location.ProvinceId, Location.Country,
EqType.EqType, Contact.Phone, Contact.Fax,
Contact.Email, Contact.UserName
FROM dbo.EqType EqType
RIGHT OUTER JOIN dbo.Equipment Equipment
ON EqType.EqTypeId = Equipment.EqTypeId
RIGHT OUTER JOIN dbo.Inventory Inventory
ON Equipment.EqId = Inventory.EqId
LEFT OUTER JOIN dbo.AcquisitionType AcquisitionType
ON Inventory.AcquisitionTypeID = AcquisitionType.AcquisitionTypeId
LEFT OUTER JOIN dbo.Location Location
ON Inventory.LocationId = Location.LocationId
LEFT OUTER JOIN dbo.Contact Contact
ON Inventory.OwnerId = Contact.ContactId
*/
select * from vEquipmentFull
order by Location, LastName, FirstName
-- now, let''s do record sorting
---- Make, Model -------------------
create table #tmp (SID int identity(1,1),
ID int)
insert into #tmp(ID)
select ID
from #inv
order by Make, Model
update #inv
set MakeModelSIdx = #tmp.SId
from #inv inner join #tmp
on #inv.ID = #tmp.id
drop table #tmp
----------------------------------------
---- CountrySIdx: Country, Province, City, Location -------------------
create table #tmp2 (SID int identity(1,1),
ID int)
insert into #tmp2(ID)
select ID
from #inv
order by Country, ProvinceId, City, Location
update #inv
set CountrySIdx = #tmp2.SId
from #inv inner join #tmp2
on #inv.ID = #tmp2.id
drop table #tmp2
----------------------------------------
---- LFNameSIdx: LName, FName -------------------
create table #tmp3 (SID int identity(1,1),
ID int)
insert into #tmp3(ID)
select ID
from #inv
order by LastName, FirstName
update #inv
set LFNameSIdx = #tmp3.SId
from #inv inner join #tmp3
on #inv.ID = #tmp3.id
drop table #tmp3
----------------------------------------
-- use transaction to hide operation from users
Select @intTransactionCountOnEntry = @@TranCount
BEGIN TRANSACTION
-- recreate table
if exists (select * from dbo.sysobjects
where id = object_id(N''[InventorySum]'')
and OBJECTPROPERTY(id, N''IsUserTable'') = 1)
drop table dbo.[InventorySum]
create table dbo.InventorySum(ID int,
InventoryId int,
Make varchar(50),
Model varchar(50),
Location varchar(50),
FirstName varchar(30),
LastName varchar(30),
AcquisitionType varchar(12),
Address varchar(50),
City varchar(50),
ProvinceId char(3),
Country varchar(50),
EqType varchar(50),
Phone varchar(20),
Fax varchar(20),
Email varchar(128),
UserName varchar(50),
MakeModelSIdx int,
LFNameSIdx int,
CountrySIdx int)
-- populate table
insert into dbo.InventorySum (ID,
InventoryId, Make, Model,
Location, FirstName, LastName,
AcquisitionType, Address, City,
ProvinceId, Country, EqType,
Phone, Fax, Email,
UserName, MakeModelSIdx, LFNameSIdx,
CountrySIdx)
select ID, InventoryId, Make, Model,
Location, FirstName, LastName,
AcquisitionType, Address, City,
ProvinceId, Country, EqType,
Phone, Fax, Email,
UserName, MakeModelSIdx, LFNameSIdx,
CountrySIdx
from #inv
-- create indexes
CREATE UNIQUE CLUSTERED INDEX [idx_InvSum_Id]
ON [dbo].[InventorySum] ([ID])
CREATE INDEX [idx_InvSum_LFName]
ON [dbo].[InventorySum] (LastName, FirstName)
CREATE INDEX [idx_InvSum_Location]
ON [dbo].[InventorySum] (Location)
CREATE INDEX [idx_InvSum_ModelMakeEqType]
ON [dbo].[InventorySum] (Model, Make, EqType)
-- complete transaction - give access to users
If @@TranCount > @intTransactionCountOnEntry
COMMIT TRANSACTION
return
'
END
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sysdiagrams]') AND type in (N'U'))
--BEGIN
--CREATE TABLE [dbo].[sysdiagrams](
-- [name] [sysname] NOT NULL,
-- [principal_id] [int] NOT NULL,
-- [diagram_id] [int] IDENTITY(1,1) NOT NULL,
-- [version] [int] NULL,
-- [definition] [varbinary](max) NULL,
--PRIMARY KEY CLUSTERED
--(
-- [diagram_id] ASC
--)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
-- CONSTRAINT [UK_principal_name] UNIQUE NONCLUSTERED
--(
-- [principal_id] ASC,
-- [name] ASC
--)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
--) ON [PRIMARY]
--END
--GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_OrdersByCountry_List]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create Procedure [dbo].[ap_OrdersByCountry_List]
@Country char(3)
With Recompile
as
Select *
from Orders
where Country = @Country
'
END
GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_Proc1]') AND type in (N'P', N'PC'))
--BEGIN
--EXEC dbo.sp_executesql @statement = N'create procedure [dbo].[ap_Proc1]
--as
--select * from T1
--'
--END
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[test]') AND type in (N'P', N'PC'))
--BEGIN
--EXEC dbo.sp_executesql @statement = N'-- VDT file name: C:\Documents and Settings\dsunderic.LG\My Documents\Visual Studio 2005\Projects\VbTriggers\VbTriggers\Test Scripts\Test.sql
--create proc [dbo].[test]
--as
--exec sp_who;
--
--'
--END
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_diagramobjects]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
--BEGIN
--execute dbo.sp_executesql @statement = N'
-- CREATE FUNCTION [dbo].[fn_diagramobjects]()
-- RETURNS int
-- WITH EXECUTE AS N''dbo''
-- AS
-- BEGIN
-- declare @id_upgraddiagrams int
-- declare @id_sysdiagrams int
-- declare @id_helpdiagrams int
-- declare @id_helpdiagramdefinition int
-- declare @id_creatediagram int
-- declare @id_renamediagram int
-- declare @id_alterdiagram int
-- declare @id_dropdiagram int
-- declare @InstalledObjects int
--
-- select @InstalledObjects = 0
--
-- select @id_upgraddiagrams = object_id(N''dbo.sp_upgraddiagrams''),
-- @id_sysdiagrams = object_id(N''dbo.sysdiagrams''),
-- @id_helpdiagrams = object_id(N''dbo.sp_helpdiagrams''),
-- @id_helpdiagramdefinition = object_id(N''dbo.sp_helpdiagramdefinition''),
-- @id_creatediagram = object_id(N''dbo.sp_creatediagram''),
-- @id_renamediagram = object_id(N''dbo.sp_renamediagram''),
-- @id_alterdiagram = object_id(N''dbo.sp_alterdiagram''),
-- @id_dropdiagram = object_id(N''dbo.sp_dropdiagram'')
--
-- if @id_upgraddiagrams is not null
-- select @InstalledObjects = @InstalledObjects + 1
-- if @id_sysdiagrams is not null
-- select @InstalledObjects = @InstalledObjects + 2
-- if @id_helpdiagrams is not null
-- select @InstalledObjects = @InstalledObjects + 4
-- if @id_helpdiagramdefinition is not null
-- select @InstalledObjects = @InstalledObjects + 8
-- if @id_creatediagram is not null
-- select @InstalledObjects = @InstalledObjects + 16
-- if @id_renamediagram is not null
-- select @InstalledObjects = @InstalledObjects + 32
-- if @id_alterdiagram is not null
-- select @InstalledObjects = @InstalledObjects + 64
-- if @id_dropdiagram is not null
-- select @InstalledObjects = @InstalledObjects + 128
--
-- return @InstalledObjects
-- END
-- '
--END
--
--GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[fnSafeDynamicString]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
BEGIN
execute dbo.sp_executesql @statement = N'CREATE FUNCTION [util].[fnSafeDynamicString]
-- make string parameters safe for use in dynamic strings
(@chvInput nvarchar(max),
@bitLikeSafe bit = 0) -- set to 1 if string will be used in LIKE
RETURNS nvarchar(max)
AS
BEGIN
declare @chvOutput nvarchar(max)
set @chvOutput = Replace(@chvInput, char(39), char(39) + char(39))
if @bitLikeSafe = 1
begin
-- convert square bracket
set @chvOutput = Replace(@chvOutput, ''['', ''[[]'')
-- convert wild cards
set @chvOutput = Replace(@chvOutput, ''%'', ''[%]'')
set @chvOutput = Replace(@chvOutput, ''_'', ''[_]'')
end
RETURN (@chvOutput)
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[ap_DataGenerator]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE proc [util].[ap_DataGenerator]
-- generate a set of Insert statements
-- that can reproduce content of the table.
-- It does not handle very very long columns.
@table sysname = ''Inventory'',
@debug int = 0
-- debug: exec util.ap_DataGenerator @table = ''Inventory'', @debug = 1
as
declare @chvVal varchar(max)
declare @chvSQL varchar(max)
declare @chvColList varchar(max)
declare @intColCount smallint
declare @i smallint
set @chvColList = ''''
set @chvVal = ''''
select @intColCount = Max([ORDINAL_POSITION]),
@i = 1
FROM [INFORMATION_SCHEMA].[COLUMNS]
where [TABLE_NAME] = @table
while @i <= @intColCount
begin
SELECT @chvVal = @chvVal
+ ''+'''',''''+case when '' + [COLUMN_NAME]
-- + '''''',''''case when '' + [COLUMN_NAME]
+ '' is null then ''''null'''' else ''
+ case when DATA_TYPE in (''varchar'', ''nvarchar'', ''datetime'',
''smalldatetime'', ''char'', ''nchar'')
then ''''''''''''''''''+convert(varchar(max),''
else ''+ convert(varchar(max),''
end
+ convert(varchar(max),[COLUMN_NAME])
+ case when DATA_TYPE in (''varchar'', ''nvarchar'', ''datetime'',
''smalldatetime'',''char'', ''nchar'')
then '')+''''''''''''''''''
else '')''
end
+ '' end ''
FROM [INFORMATION_SCHEMA].[COLUMNS]
where [TABLE_NAME] = @table
and [ORDINAL_POSITION] = @i
-- if @debug <> 0 select @chvVal [@chvVal]
-- get column list
SELECT @chvColList = @chvColList
+ '','' + convert(varchar(max),[COLUMN_NAME])
FROM [INFORMATION_SCHEMA].[COLUMNS]
where [TABLE_NAME] = @table
and [ORDINAL_POSITION] = @i
set @i = @i + 1
end
if @debug <> 0 select @chvColList [@chvColList]
if @debug <> 0 select @chvVal [@chvVal]
-- remove first comma
set @chvColList = substring(@chvColList, 2, len(@chvColList))
set @chvVal = substring(@chvVal, 6, len(@chvVal))
-- assemble a command to query the table to assemble everything
set @chvSQL = ''select ''''Insert dbo.'' + @table
+ ''('' + @chvColList +'') values (''''+''
+ @chvVal + '' + '''')''''from '' +@table
-- get result
if @debug <> 0 select @chvSQL chvSQL
exec(@chvSQL)
return'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_LeasePeriodDuration_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'Create Procedure [dbo].[ap_LeasePeriodDuration_Get]
-- return approximate number of days associated with lease frequency
@inyScheduleFrequencyId tinyint,
@insDays smallint OUTPUT
As
Declare @chvScheduleFrequency varchar(50)
Select @chvScheduleFrequency = ScheduleFrequency
From dbo.ScheduleFrequency
where ScheduleFrequencyId = @inyScheduleFrequencyId
select @insDays =
Case @chvScheduleFrequency
When ''monthly'' then 30
When ''semi-monthly'' then 15
When ''bi-weekly'' then 14
When ''weekly'' then 7
When ''quarterly'' then 92
When ''yearly'' then 365
END
return
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ap_InventoryProperties_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [dbo].[ap_InventoryProperties_Get]
/************************************************************
Return comma-delimited list of properties that are describing asset.
i.e.: Property = Value Unit;Property = Value Unit;Property = Value
Unit;Property = Value Unit;Property = Value Unit;...
test:
declare @p varchar(max)
exec ap_InventoryProperties_Get 5, @p OUTPUT, 1
select @p
*************************************************************/
(
@intInventoryId int,
@chvProperties varchar(max) OUTPUT,
@debug int = 0
)
As
declare @intCountProperties int,
@intCounter int,
@chvProperty varchar(50),
@chvValue varchar(50),
@chvUnit varchar(50),
@chvProcedure sysname
set @chvProcedure = ''ap_InventoryProperties_Get''
if @debug <> 0
select ''**** ''+ @chvProcedure + ''START ****''
Create table #Properties(
Id int identity(1,1),
Property varchar(50),
Value varchar(50),
Unit varchar(50))
-- identify Properties associated with asset
insert into #Properties (Property, Value, Unit)
select Property, Value, Unit
from dbo.InventoryProperty InventoryProperty
inner join dbo.Property Property
on InventoryProperty.PropertyId = Property.PropertyId
where InventoryProperty.InventoryId = @intInventoryId
if @debug = 1
select * from #Properties
-- set loop
select @intCountProperties = Count(*),
@intCounter = 1,
@chvProperties = ''''
from #Properties
-- loop through list of properties
while @intCounter <= @intCountProperties
begin
-- get one property
select @chvProperty = Property,
@chvValue = Value,
@chvUnit = Unit
from #Properties
where Id = @intCounter
if @debug <> 0
select @chvProperty Property,
@chvValue [Value],
@chvUnit [Unit]
-- assemble list
set @chvProperties = @chvProperties + ''; ''
+ @chvProperty + ''=''
+ @chvValue + '' '' + ISNULL(@chvUnit, '''')
-- let''s go another round and get another property
set @intCounter = @intCounter + 1
end
if Substring(@chvProperties, 0, 2) = ''; ''
set @chvProperties = Right(@chvProperties, Len(@chvProperties) - 2)
drop table #Properties
if @debug <> 0
select ''**** ''+ @chvProcedure + ''END ****''
return 0
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[ap_TempTbl2Varchar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'create Procedure [util].[ap_TempTbl2Varchar]
-- Convert information from #List temporary table to a single varchar
@chvResult varchar(max) output
As
set nocount on
declare @intCountItems int,
@intCounter int,
@chvItem varchar(255)
-- set loop
select @intCountItems = Count(*),
@intCounter = 1,
@chvResult = ''''
from #List
-- loop through list of items
while @intCounter <= @intCountItems
begin
-- get one property
select @chvItem = Item
from #List
where Id = @intCounter
-- assemble list
set @chvResult = @chvResult + @chvItem
-- let''s go another round and get another item
set @intCounter = @intCounter + 1
end
return 0
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[ap_Cursor2Varchar]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE Procedure [util].[ap_Cursor2Varchar]
-- Process information from cursor initiated in calling sp.
-- Convert records into a single varchar.
(
@chvResult varchar(max) OUTPUT,
@debug int = 0
)
As
Declare @chvItem varchar(255)
set @chvResult = ''''
Fetch Next From curItems
Into @chvItem
While (@@FETCH_STATUS = 0)
Begin
If @debug <> 0
Select @chvItem Item
-- assemble list
Set @chvResult = @chvResult + @chvItem
If @debug <> 0
Select @chvResult chvResult
Fetch Next From curItems
Into @chvItem
End