-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamProcedure.sql
1327 lines (996 loc) · 41.3 KB
/
ExamProcedure.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
USE [master]
GO
/****** Object: Database [PremierDataWarehouse] Script Date: 02/24/18 8:47:24 AM ******/
CREATE DATABASE [PremierDataWarehouse]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'PremierDataWarehouse', FILENAME = N'N:\SQLData\PremierDataWarehouse.mdf' , SIZE = 2412544KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'PremierDataWarehouse_log', FILENAME = N'N:\SQLData\PremierDataWarehouse_log.ldf' , SIZE = 13217792KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [PremierDataWarehouse] SET COMPATIBILITY_LEVEL = 120
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [PremierDataWarehouse].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [PremierDataWarehouse] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET ANSI_NULLS OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET ANSI_PADDING OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET ARITHABORT OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [PremierDataWarehouse] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [PremierDataWarehouse] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET DISABLE_BROKER
GO
ALTER DATABASE [PremierDataWarehouse] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [PremierDataWarehouse] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET RECOVERY SIMPLE
GO
ALTER DATABASE [PremierDataWarehouse] SET MULTI_USER
GO
ALTER DATABASE [PremierDataWarehouse] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [PremierDataWarehouse] SET DB_CHAINING OFF
GO
ALTER DATABASE [PremierDataWarehouse] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [PremierDataWarehouse] SET TARGET_RECOVERY_TIME = 0 SECONDS
GO
ALTER DATABASE [PremierDataWarehouse] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [PremierDataWarehouse] SET READ_WRITE
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_backend_login] Script Date: 02/24/18 8:48:44 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_backend_login](@username varchar(50), @password varchar(100))
as
begin
declare @tmp varchar(100), @status tinyint, @EmployeeISN int
select @tmp=empPassword, @status=empStatus, @EmployeeISN=EmployeeISN from Employee
where empUserName=@username
if(@tmp is null) return -4
if(@status=0) return -3
if(@tmp<>@password) return -2
return @employeeisn
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_calculatehistory_getpage] Script Date: 02/24/18 8:49:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_calculatehistory_getpage](@condition nvarchar(3000)='', @nItemPage int=20, @curpage int=1, @NoRec int=null,
@SessionID varchar(50)=null, @sortBy varchar(100)='HistoryISN', @sortDirect varchar(10)='DESC')
as
begin
declare @tableOrview varchar(100)
set @tableOrview='Vw_CalculateHistory'
declare @qry nvarchar(4000), @n int
if(@condition<>'') set @condition= ' where ' + @condition
if(@NoRec is null or @NoRec<=0)
begin
create table #tmp(NoRec int)
set @qry='insert into #tmp(NoRec) select count(*) from ' + @tableOrview + ' ' + @condition
exec(@qry); select @norec=NoRec from #tmp
drop table #tmp
END
declare @minISN int, @maxISN int
set @minISN=(@curpage-1)*@nItemPage + 1
set @maxISN=@curpage*@nItemPage
set @qry='select a.* from
(
SELECT ROW_NUMBER() OVER (ORDER BY ' + @sortBy + ' ' + @sortDirect + ') as ROWID, a.*
from ' + @tableOrview + ' a ' + @condition + '
) as a
where RowID between ' + cast(@minISN AS varchar) + ' and ' + cast(@maxISN AS varchar) + ' order by RowID'
print @qry
exec (@qry)
select @norec as NoOfRec
return @norec
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_calculatehistory_ins] Script Date: 02/24/18 8:49:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_calculatehistory_ins](@PercentSalesRepresentative float, @NewLeadPerSalesRepresentativeDay float,
@OpenerAppConversionRate float, @CallstoOpenerDay float, @WorkdaysWeek float, @NoOfSalesRepresentative float,
@NoOfOpenerNeeded float, @TotalCallsWeek float, @DebtLoadDesc nvarchar(2000)=null, @CountISN int, @TotalPieces int,
@Details varchar(8000), @updatedBy int)
as
begin
declare @HistoryISN int, @idoc int
declare @tbDetail as table (SettingID int, Leads int, MailPercent float, PieceQty int)
exec sp_xml_preparedocument @idoc OUTPUT, @Details
Insert Into @tbDetail(SettingID, Leads, MailPercent, PieceQty)
select SettingID, Leads, MailPercent, PieceQty
from OPENXML(@idoc, '/Root/Detail')
WITH (SettingID int, Leads int, MailPercent float, PieceQty int)
exec sp_xml_removedocument @idoc
Insert Into CalculateHistory(PercentSalesRepresentative, NewLeadPerSalesRepresentativeDay, OpenerAppConversionRate, CallstoOpenerDay,
WorkdaysWeek, NoOfSalesRepresentative, NoOfOpenerNeeded, TotalCallsWeek, DebtLoadDesc, CountISN, TotalPieces, updatedBy)
Values(@PercentSalesRepresentative, @NewLeadPerSalesRepresentativeDay, @OpenerAppConversionRate, @CallstoOpenerDay,
@WorkdaysWeek, @NoOfSalesRepresentative, @NoOfOpenerNeeded, @TotalCallsWeek, @DebtLoadDesc, @CountISN, @TotalPieces, @updatedBy)
set @HistoryISN=SCOPE_IDENTITY()
Insert Into [dbo].[CalculateHistoryDetail](HistoryISN, SettingID, DebtLoad, LeadProjected, MinAmount, MaxAmount, Leads, MailPercent, PieceQty, updatedBy)
select @HistoryISN, a.SettingID, b.DebtLoad, b.LeadProjected, b.MinAmount, b.MaxAmount, a.Leads, a.MailPercent, a.PieceQty, @updatedBy
from @tbDetail a, RateSetting b
where a.SettingID=b.SettingID
return @HistoryISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_datacount_getpage] Script Date: 02/24/18 8:50:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_datacount_getpage](@condition nvarchar(3000)='', @nItemPage int=20, @curpage int=1, @NoRec int=null,
@SessionID varchar(50)=null, @sortBy varchar(100)='CountISN', @sortDirect varchar(10)='DESC')
as
begin
declare @tableOrview varchar(100)
set @tableOrview='Vw_DataCount'
declare @qry nvarchar(4000), @n int
if(@condition<>'') set @condition= ' where ' + @condition
if(@NoRec is null or @NoRec<=0)
begin
create table #tmp(NoRec int)
set @qry='insert into #tmp(NoRec) select count(*) from ' + @tableOrview + ' ' + @condition
exec(@qry); select @norec=NoRec from #tmp
drop table #tmp
END
declare @minISN int, @maxISN int
set @minISN=(@curpage-1)*@nItemPage + 1
set @maxISN=@curpage*@nItemPage
set @qry='select a.* from
(
SELECT ROW_NUMBER() OVER (ORDER BY ' + @sortBy + ' ' + @sortDirect + ') as ROWID, a.*
from ' + @tableOrview + ' a ' + @condition + '
) as a
where RowID between ' + cast(@minISN AS varchar) + ' and ' + cast(@maxISN AS varchar) + ' order by RowID'
print @qry
exec (@qry)
select @norec as NoOfRec
return @norec
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_datacount_getaverage] Script Date: 02/24/18 8:50:29 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Proc [dbo].[xp_datacount_getaverage](@CountISN int)
as
begin
declare @TotalQty float, @TotalDebt1Qty int, @TotalDebt2Qty int, @Debt1Percent float, @Debt2Percent float
select @TotalDebt1Qty=sum(Debt1Qty), @TotalDebt2Qty=sum(Debt2Qty)
from DataCountProviderDetailFile
where CountISN=@CountISN
set @TotalDebt1Qty=isnull(@TotalDebt1Qty, 0)
set @TotalDebt2Qty=isnull(@TotalDebt2Qty, 0)
set @TotalQty = @TotalDebt1Qty + @TotalDebt2Qty
if(@TotalQty=0) return -2
set @Debt1Percent = @TotalDebt1Qty/@TotalQty
set @Debt2Percent = @TotalDebt2Qty/@TotalQty
select @TotalDebt1Qty as TotalDebt1Qty, @TotalDebt2Qty as TotalDebt2Qty, @TotalQty as TotalQty,
@Debt1Percent as Debt1Percent, @Debt2Percent as Debt2Percent
return 0;
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_datacount_muldel] Script Date: 02/24/18 8:51:49 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_datacount_muldel](@lstISNs varchar(8000))
as
begin
declare @ObjISN int, @ISN int, @Name nvarchar(1000)
Create Table #temp(ISN int, [Name] nvarchar(1000), Code varchar(200))
Create Table #tempISN(ISN int identity, Info int)
exec xp_string_split @lstISNs, '#tempISN'
declare curdel cursor local for
select ISN, convert(int,Info) from #tempISN order by ISN
open curdel
fetch next from curdel into @isn, @ObjISN
while(@@fetch_status=0)
begin
if exists(select * from DataCount where CountISN=@ObjISN and TotalReceivedQty>0)
begin
select @Name=CountName from DataCount where CountISN=@ObjISN
Insert Into #temp(ISN, [Name], Code) values(@ObjISN, @Name, -2)
break
end
delete from DataCount where CountISN=@ObjISN
fetch next from curdel into @isn, @ObjISN
end
close curdel
deallocate curdel
select * from #temp;
drop table #tempISN
drop table #temp
return 0
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_datacount_insupd] Script Date: 02/24/18 8:51:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_datacount_insupd](@CountISN int=null, @CountName nvarchar(100)=null, @CriteriaFiles varchar(500)=null,
@ProviderTotalRecords int=null, @ProviderFiles varchar(500)=null, @ProviderFileContent nvarchar(max)=null,
@Description nvarchar(500)=null, @OrderFileName varchar(100)=null, @DataReceivedFiles varchar(500)=null, @Status tinyint=null, @updatedBy int)
as
begin
set @CountISN=isnull(@CountISN, 0)
if(@CountISN<=0)
begin
Insert Into DataCount(CountName, CriteriaFiles, ProviderTotalRecords, ProviderFiles, [Description], addedBy, updatedBy)
Values(@CountName, @CriteriaFiles, @ProviderTotalRecords, @ProviderFiles, @Description, @updatedBy, @updatedBy)
set @CountISN=SCOPE_IDENTITY()
end
else
begin
update DataCount
set CountName=isnull(@CountName, CountName),
CriteriaFiles=isnull(@CriteriaFiles, CriteriaFiles),
ProviderTotalRecords=isnull(@ProviderTotalRecords, ProviderTotalRecords),
ProviderFiles=isnull(@ProviderFiles, ProviderFiles),
[Description]=isnull(@Description, [Description]),
OrderFileName=isnull(@OrderFileName, OrderFileName),
DataReceivedFiles=isnull(@DataReceivedFiles, DataReceivedFiles),
[Status]=isnull(@Status, [Status]),
updatedDate=getdate(), updatedBy=@updatedBy
where CountISN=@CountISN
end
if(@ProviderFileContent is not null and @ProviderFileContent<>'')
begin
declare @idoc int
declare @tbFileContent as table([State] varchar(50), [Debt1Qty] int, [Debt1Percent] float, [Debt2Qty] int, [Debt2Percent] float,
[Debt3Qty] int, [Debt3Percent] float, TotalQty int)
exec sp_xml_preparedocument @idoc OUTPUT, @ProviderFileContent
Insert Into @tbFileContent([State], Debt1Qty, Debt1Percent, Debt2Qty, Debt2Percent, Debt3Qty, Debt3Percent, TotalQty)
select [State], Debt1Qty, Debt1Percent, Debt2Qty, Debt2Percent, Debt3Qty, Debt3Percent, TotalQty
from OPENXML(@idoc, '/Root/Info')
WITH (State varchar(50), Debt1Qty int, Debt1Percent float, Debt2Qty int, Debt2Percent float, Debt3Qty int, Debt3Percent float, TotalQty int)
exec sp_xml_removedocument @idoc
delete from DataCountProviderDetailFile where CountISN=@CountISN
Insert Into DataCountProviderDetailFile(CountISN, [State], Debt1Qty, Debt1Percent, Debt2Qty, Debt2Percent, Debt3Qty, Debt3Percent, TotalQty, updatedBy)
select @CountISN, [State], Debt1Qty, Debt1Percent, Debt2Qty, Debt2Percent, Debt3Qty, Debt3Percent, TotalQty, @updatedBy
from @tbFileContent
where Debt1Qty>0 and Debt2Qty>0
select @ProviderTotalRecords=sum( isnull(Debt1Qty,0) + isnull(Debt2Qty,0) + isnull(Debt3Qty,0) )
from DataCountProviderDetailFile
where CountISN=@CountISN
update DataCount set ProviderTotalRecords=@ProviderTotalRecords where CountISN=@CountISN
end
return @CountISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_dataexport_del] Script Date: 02/24/18 8:52:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_dataexport_del](@ExportISN int)
as
begin
if exists(select * from DataExport where ExportISN=@ExportISN and PieceQty is not null and PieceQty>0)
return -2
if not exists(select * from DataExport where ExportISN=@ExportISN and ExportDate>dbo.DateOnly(getdate()-7))
return -3
update Member
set LastExportDate=null
where MemberISN in(select MemberISN from MemberOfExport where ExportISN=@ExportISN)
delete from MemberOfExport where ExportISN=@ExportISN
delete from DataExport where ExportISN=@ExportISN
return 0
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_dataexport_getpage] Script Date: 02/24/18 9:10:28 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_dataexport_getpage](@condition nvarchar(3000)='', @nItemPage int=20, @curpage int=1, @NoRec int=null,
@SessionID varchar(50)=null, @sortBy varchar(100)='ExportISN', @sortDirect varchar(10)='DESC')
as
begin
declare @tableOrview varchar(100)
set @tableOrview='Vw_DataExport'
declare @qry nvarchar(4000), @n int
if(@condition<>'') set @condition= ' where ' + @condition
if(@NoRec is null or @NoRec<=0)
begin
create table #tmp(NoRec int)
set @qry='insert into #tmp(NoRec) select count(*) from ' + @tableOrview + ' ' + @condition
exec(@qry); select @norec=NoRec from #tmp
drop table #tmp
END
declare @minISN int, @maxISN int
set @minISN=(@curpage-1)*@nItemPage + 1
set @maxISN=@curpage*@nItemPage
set @qry='select a.* from
(
SELECT ROW_NUMBER() OVER (ORDER BY ' + @sortBy + ' ' + @sortDirect + ') as ROWID, a.*
from ' + @tableOrview + ' a ' + @condition + '
) as a
where RowID between ' + cast(@minISN AS varchar) + ' and ' + cast(@maxISN AS varchar) + ' order by RowID'
print @qry
exec (@qry)
select @norec as NoOfRec
return @norec
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_dataexport_upd2] Script Date: 02/24/18 9:10:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_dataexport_upd2](@ExportISN int, @CampaignPrefix nvarchar(20)=null, @CodeLength int=null, @CodeStart int=null,
@IVRName nvarchar(50)=null, @DDSessionID varchar(100)=null, @JobNumber varchar(50)=null, @MailingDate datetime='1/1/1900', @updatedBy int)
as
begin
update DataExport
set IVRName=isnull(@IVRName, IVRName),
DDSessionID=isnull(@DDSessionID, DDSessionID),
JobNumber=isnull(@JobNumber, JobNumber),
MailingDate= (case when @MailingDate='1/1/1900' then MailingDate else @MailingDate end),
CampaignPrefix=isnull(@CampaignPrefix, CampaignPrefix),
CodeLength=isnull(@CodeLength, CodeLength),
CodeStart=isnull(@CodeStart, CodeStart),
updatedDate=getdate(), updatedBy=@updatedBy
where ExportISN=@ExportISN
return @ExportISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_dataorder_generate] Script Date: 02/24/18 9:11:19 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_dataorder_generate](@CountISN int, @Order1Qty int, @Order2Qty int, @updatedBy int)
as
begin
declare @Debt1TotalQty int, @Debt2TotalQty int, @Percent float
--if exists(select * from DataCount where CountISN=@CountISN and TotalReceivedQty>0)
-- return -4
declare @tmpProviderFile as table ([State] varchar(50), [Debt1Qty] int, [Debt2Qty] int, [Order1Qty] int default 0, [Order2Qty] int default 0,
Debt1Percent float, Debt2Percent float)
Insert Into @tmpProviderFile([State], [Debt1Qty], [Debt2Qty], Debt1Percent, Debt2Percent)
select [State], [Debt1Qty], [Debt2Qty], Debt1Percent, Debt2Percent
from DataCountProviderDetailFile
where CountISN=@CountISN
select @Debt1TotalQty=sum([Debt1Qty]), @Debt2TotalQty=sum([Debt2Qty]) from @tmpProviderFile
if(@Order1Qty<=0 and @Order2Qty<=0) return -3
if(@Debt1TotalQty<@Order1Qty or @Debt2TotalQty<@Order2Qty) return -2
declare @State varchar(20), @StoreQty int, @Qty int, @RemainTotalQty int, @NoRec int, @Count int
-- Order1 Qty
if(@Order1Qty>0)
begin
if(@Order1Qty=@Debt1TotalQty)
begin
update @tmpProviderFile set Order1Qty=Debt1Qty
end
else
begin
select @NoRec = count(*) from @tmpProviderFile where [Debt1Qty]>0
set @Count=1
set @RemainTotalQty=@Order1Qty
declare cur1 cursor local for
select [State], [Debt1Qty], Debt1Percent from @tmpProviderFile where Debt1Percent>0 order by Debt1Percent
open cur1
fetch next from cur1 into @State, @StoreQty, @Percent
while(@@FETCH_STATUS=0)
begin
if(@Count<@NoRec)
begin
set @Qty = round(@Order1Qty*@Percent,0)
if(@Qty>@StoreQty) set @Qty=@StoreQty
if(@Qty>@RemainTotalQty) set @Qty=@RemainTotalQty
update @tmpProviderFile set Order1Qty=@Qty where [State]=@State
set @RemainTotalQty = @RemainTotalQty - @Qty
if(@RemainTotalQty<=0)
begin
print @RemainTotalQty
break;
end
end
else
begin
update @tmpProviderFile set Order1Qty=@RemainTotalQty where [State]=@State
end
set @Count=@Count+1
fetch next from cur1 into @State, @StoreQty, @Percent
end
close cur1
deallocate cur1
end
end
-- Order2 Qty
if(@Order2Qty>0)
begin
if(@Order2Qty=@Debt2TotalQty)
begin
update @tmpProviderFile set Order2Qty=Debt2Qty
end
else
begin
select @NoRec = count(*) from @tmpProviderFile where Debt2Qty>0
set @Count=1
set @RemainTotalQty=@Order2Qty
declare cur1 cursor local for
select [State], Debt2Qty, Debt2Percent from @tmpProviderFile where Debt2Percent>0 order by Debt2Percent
open cur1
fetch next from cur1 into @State, @StoreQty, @Percent
while(@@FETCH_STATUS=0)
begin
if(@Count<@NoRec)
begin
set @Qty = round(@Order2Qty*@Percent,0)
if(@Qty>@StoreQty) set @Qty=@StoreQty
if(@Qty>@RemainTotalQty) set @Qty=@RemainTotalQty
update @tmpProviderFile set Order2Qty=@Qty where [State]=@State
set @RemainTotalQty = @RemainTotalQty - @Qty
if(@RemainTotalQty<=0)
begin
print @RemainTotalQty
break;
end
end
else
begin
update @tmpProviderFile set Order2Qty=@RemainTotalQty where [State]=@State
end
set @Count=@Count+1
fetch next from cur1 into @State, @StoreQty, @Percent
end
close cur1
deallocate cur1
end
end
delete from DataOrderDetail where CountISN=@CountISN
Insert Into DataOrderDetail(CountISN, [State], [Debt1Qty], [Debt2Qty], updatedBy)
select @CountISN, [State], [Order1Qty], [Order2Qty], @updatedBy from @tmpProviderFile
update Datacount
set Order1Qty=@Order1Qty, Order2Qty=@Order2Qty,
TotalOrderQty=@Order1Qty+@Order2Qty,
OrderFileName='',
updatedDate=getdate(), updatedBy=@updatedBy
where CountISN=@CountISN
return @CountISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_dataexportdetail_upd] Script Date: 02/24/18 9:11:16 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_dataexportdetail_upd](@DetailISN int, @ExportFileName varchar(100)=null,
@CampaignName nvarchar(50)=null, @CampaignDID varchar(20)=null, @PieceQty int=null,
@ROICampaignISN int=null, @DDSessionID varchar(100)=null, @DDCampaignISN int=-1, @updatedBy int)
as
begin
declare @ExportISN int, @TotalPieceQty int, @ExportFileNames varchar(2000), @CampaignNames nvarchar(2000),
@PieceQtyDetail varchar(200), @CampaignDIDs varchar(200), @DDSessionIDs varchar(2000), @DetailISNs varchar(500)
select @ExportISN=ExportISN from DataExportDetail where DetailISN=@DetailISN
update DataExportDetail
set ExportFileName=isnull(@ExportFileName, ExportFileName),
CampaignName=isnull(@CampaignName, CampaignName),
CampaignDID=isnull(@CampaignDID, CampaignDID),
PieceQty=isnull(@PieceQty, PieceQty),
ROICampaignISN=isnull(@ROICampaignISN, ROICampaignISN),
DDSessionID=isnull(@DDSessionID, DDSessionID),
DDCampaignISN=(case when @DDCampaignISN>0 then @DDCampaignISN else DDCampaignISN end),
updatedDate=getdate(), updatedBy=@updatedBy
where DetailISN=@DetailISN
create table #tmpdetail(ExportFileName varchar(100), CampaignName nvarchar(50), CampaignDID varchar(20), PieceQty int, DDSessionID varchar(100), DetailISN int primary key)
Insert Into #tmpdetail(ExportFileName, CampaignName, CampaignDID, PieceQty, DDSessionID, DetailISN)
select ExportFileName, CampaignName, CampaignDID, PieceQty, DDSessionID, DetailISN
from DataExportDetail
where ExportISN=@ExportISN and DebtQty>0
select @TotalPieceQty=sum(PieceQty) from #tmpdetail
set @ExportFileNames = STUFF( (SELECT '»' + ExportFileName FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
set @CampaignNames = STUFF( (SELECT '»' + CampaignName FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
set @CampaignDIDs = STUFF( (SELECT '»' + CampaignDID FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
set @PieceQtyDetail = STUFF( (SELECT '»' + convert(varchar(20), PieceQty) FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
set @DDSessionIDs = STUFF( (SELECT '»' + DDSessionID FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
set @DetailISNs = STUFF( (SELECT '»' + convert(varchar(20), DetailISN) FROM #tmpdetail FOR XML PATH ('') ), 1, 1, '')
update DataExport
set PieceQty=@TotalPieceQty, ExportFileName=@ExportFileNames,
CampaignName=@CampaignNames, PieceQtyDetail=@PieceQtyDetail,
CampaignDID=@CampaignDIDs,
DDSessionID=@DDSessionIDs, DetailISNs=@DetailISNs
where ExportISN=@ExportISN
return @DetailISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_inventory_export2] Script Date: 02/24/18 9:12:24 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_inventory_export2](@Debt1Qty int, @Debt2Qty int, @Debt3Qty int, @Debt4Qty int, @Debt5Qty int, @ExpDesc nvarchar(2000)=null,
@CalculateISN int=null, @UpdatedBy int, @dx int=90)
as
begin
declare @Until datetime, @TotalQty int, @Debt1TotalQty int, @Debt2TotalQty int, @Debt3TotalQty int,
@Debt4TotalQty int, @Debt5TotalQty int, @Qry varchar(max), @ExportISN int
set @Until=dbo.DateOnly(getdate() - @dx)
if exists( select * from DataExport where ExportStatus=0 and ExportDate>DATEADD(mi, -30, getdate()) )
return -2
Insert Into DataExport(ExportBy, ExpDesc, updatedBy) Values(@updatedBy, @ExpDesc, @updatedBy)
set @ExportISN = SCOPE_IDENTITY();
declare @reqExport as table(SettingID int, DebtQty int, OrderQty int default 0)
Insert Into @reqExport(SettingID, DebtQty) Values(1, @Debt1Qty)
Insert Into @reqExport(SettingID, DebtQty) Values(2, @Debt2Qty)
Insert Into @reqExport(SettingID, DebtQty) Values(3, @Debt3Qty)
Insert Into @reqExport(SettingID, DebtQty) Values(4, @Debt4Qty)
Insert Into @reqExport(SettingID, DebtQty) Values(5, @Debt5Qty)
declare @Setting as table(SettingID int primary key, MinAmount money, MaxAmount money)
Insert Into @Setting(SettingID, MinAmount, MaxAmount)
select SettingID, isnull(MinAmount,0), isnull(MaxAmount, 100000000) from RateSetting
declare @tmpSummary as table ([State] varchar(50), SettingID int, StoreQty int)
Insert Into @tmpSummary([State], SettingID, StoreQty)
select memState, SettingID, count(*)
from Member a, @Setting b
where (LastExportDate<@Until or LastExportDate is null)
and a.memESTDebt>=b.MinAmount and a.memESTDebt<b.MaxAmount
group by memState, SettingID
declare @MinAmount money, @MaxAmount money, @DebtQty int, @OrderQty int, @SettingID int, @StoreTotalQty int,
@State varchar(20), @StoreQty int, @Qty int, @RemainTotalQty int, @NoRec int, @Count int
Create Table #tmpISN(MemberISN int primary key, SettingID int)
declare curexport cursor local for
select SettingID, DebtQty from @reqExport where DebtQty>0
open curexport
fetch next from curexport into @SettingID, @DebtQty
while(@@FETCH_STATUS=0)
begin
set @OrderQty=0
select @StoreTotalQty=sum(StoreQty) from @tmpSummary where SettingID=@SettingID
select @MinAmount=MinAmount, @MaxAmount=MaxAmount from @Setting where SettingID=@SettingID
if(@DebtQty>@StoreTotalQty)
begin
set @Qry='Insert Into #tmpISN(MemberISN, SettingID) select MemberISN, ' + convert(varchar(20), @SettingID) + ' from Member where memESTDebt>=' + convert(varchar(20), @MinAmount) + ' and memESTDebt<' + convert(varchar(20), @MaxAmount) + ' and ( LastExportDate is null or LastExportDate<''' + Convert(varchar(30), @Until, 101) + ''' )'
set @OrderQty = (@DebtQty-@StoreTotalQty)
print @Qry
--exec (@Qry)
end
else
begin
select @NoRec = count(*) from @tmpSummary where StoreQty>0 and SettingID=@SettingID
set @Count=1
set @RemainTotalQty=@DebtQty
declare cur1 cursor local for
select [State], StoreQty from @tmpSummary where StoreQty>0 and SettingID=@SettingID order by StoreQty
open cur1
fetch next from cur1 into @State, @StoreQty
while(@@FETCH_STATUS=0)
begin
if(@Count<@NoRec)
begin
set @Qty = round(convert(float,@DebtQty)*@StoreQty/@StoreTotalQty,0)
if (@Qty>0)
begin
if(@Qty>@StoreQty) set @Qty=@StoreQty
if(@Qty>@RemainTotalQty) set @Qty=@RemainTotalQty
set @Qry='Insert Into #tmpISN(MemberISN, SettingID) select top ' + convert(varchar(20), @Qty) + ' MemberISN, ' + convert(varchar(20), @SettingID) + ' from Member where memESTDebt>=' + convert(varchar(20), @MinAmount) + ' and memESTDebt<' + convert(varchar(20), @MaxAmount) + ' and ( LastExportDate is null or LastExportDate<''' + Convert(varchar(30), @Until, 101) + ''' ) and memState=''' + @State + ''' order by LastExportDate desc '
print @Qry
--exec (@Qry)
set @RemainTotalQty = @RemainTotalQty - @Qty
end
if(@RemainTotalQty=0) break;
end
else
begin
if(@Qry<>'') set @Qry = @Qry + char(13) + char(10) + ' union ' + char(13) + char(10)
set @Qry='Insert Into #tmpISN(MemberISN, SettingID) select top ' + convert(varchar(20), @RemainTotalQty) + ' MemberISN, ' + convert(varchar(20), @SettingID) + ' from Member where memESTDebt>=' + convert(varchar(20), @MinAmount) + ' and memESTDebt<' + convert(varchar(20), @MaxAmount) + ' and ( LastExportDate is null or LastExportDate<''' + Convert(varchar(30), @Until, 101) + ''' ) and memState=''' + @State + ''' order by LastExportDate desc '
print @Qry
--exec (@Qry)
end
set @Count=@Count+1
fetch next from cur1 into @State, @StoreQty
end
close cur1
deallocate cur1
end
update @reqExport set OrderQty=@OrderQty where SettingID=@SettingID
fetch next from curexport into @SettingID, @DebtQty
end
close curexport
deallocate curexport
declare @tmpexportqty as table (SettingID int, DebtQty int)
Insert Into @tmpexportqty(SettingID, DebtQty)
select SettingID, count(*)
from #tmpISN
group by SettingID
update Member
set LastExportISN=@ExportISN, LastExportDate=getdate()
where MemberISN in(select MemberISN from #tmpISN)
Insert Into MemberOfExport(MemberISN, ExportISN) select MemberISN, @ExportISN from #tmpISN
Insert Into DataExportDetail(ExportISN, SettingID, DebtQty, OrderQty)
select @ExportISN, a.SettingID, a.DebtQty, b.OrderQty
from @tmpexportqty a, @reqExport b
where a.SettingID=b.SettingID
update DataExport
set ExpTotalQty = ( select sum(DebtQty) from @tmpexportqty ),
ExportStatus = 1
where ExportISN=@ExportISN
return @ExportISN
end
GO
USE [PremierDataWarehouse]
GO
/****** Object: StoredProcedure [dbo].[xp_inventory_export] Script Date: 02/24/18 9:12:20 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Proc [dbo].[xp_inventory_export](@xmlRequest nvarchar(2000), @ExpDesc nvarchar(2000)=null, @CalculateISN int=null, @UpdatedBy int, @dx int=90)
as
begin
declare @Until datetime, @Qry varchar(max), @ExportISN int, @idoc int
set @Until=dbo.DateOnly(getdate() - @dx)
if exists( select * from DataExport where ExportStatus=0 and ExportDate>DATEADD(mi, -5, getdate()) )
return -2
Insert Into DataExport(ExportBy, ExpDesc, updatedBy) Values(@updatedBy, @ExpDesc, @updatedBy)
set @ExportISN = SCOPE_IDENTITY();
declare @reqExport as table(SettingID int, DebtQty int, OrderQty int default 0)
exec sp_xml_preparedocument @idoc OUTPUT, @xmlRequest
Insert Into @reqExport(SettingID, DebtQty)
select SettingID, DebtQty
from OPENXML(@idoc, '/Root/Info')
WITH (SettingID int, DebtQty int)
exec sp_xml_removedocument @idoc
Create table #tmpSetting(SettingID int primary key, DebtLoad nvarchar(200), MinAmount money, MaxAmount money)
Insert Into #tmpSetting(SettingID, DebtLoad, MinAmount, MaxAmount)
select SettingID, DebtLoad, isnull(MinAmount,0), (case when MaxAmount<=0 or MaxAmount is null then 100000000 else MaxAmount end)
from RateSetting
Create table #tmpSummary([State] varchar(50), SettingID int, StoreQty int)
Insert Into #tmpSummary([State], SettingID, StoreQty)
select memState, SettingID, count(*)
from Member a, #tmpSetting b
where (LastExportDate<@Until or LastExportDate is null)
and a.memESTDebt>=b.MinAmount and a.memESTDebt<=b.MaxAmount
group by memState, SettingID
Create table #tmpSummary2 (SettingID int, StoreQty int)
Insert Into #tmpSummary2(SettingID, StoreQty)
select SettingID, sum(StoreQty) from #tmpSummary group by SettingID
if exists(select * from @reqExport a left join #tmpSummary2 b on a.SettingID=b.SettingID where DebtQty>isnull(b.StoreQty,0))
begin
delete from DataExport where ExportISN=@ExportISN
return -3
end
declare @MinAmount money, @MaxAmount money, @DebtQty int, @OrderQty int, @SettingID int, @StoreTotalQty int,
@State varchar(20), @StoreQty int, @Qty int, @RemainTotalQty int, @NoRec int, @Count int
Create Table #tmpISN(MemberISN int primary key, SettingID int)
declare curexport cursor local for
select SettingID, DebtQty from @reqExport where DebtQty>0
open curexport
fetch next from curexport into @SettingID, @DebtQty
while(@@FETCH_STATUS=0)
begin
set @OrderQty=0
select @StoreTotalQty=StoreQty from #tmpSummary2 where SettingID=@SettingID
select @MinAmount=MinAmount, @MaxAmount=MaxAmount from #tmpSetting where SettingID=@SettingID
if(@DebtQty>@StoreTotalQty)
begin
set @Qry='Insert Into #tmpISN(MemberISN, SettingID) select MemberISN, ' + convert(varchar(20), @SettingID) + ' from Member where memESTDebt>=' + convert(varchar(20), @MinAmount) + ' and memESTDebt<=' + convert(varchar(20), @MaxAmount) + ' and ( LastExportDate is null or LastExportDate<''' + Convert(varchar(30), @Until, 101) + ''' )'
set @OrderQty = (@DebtQty-@StoreTotalQty)
--print @Qry
exec (@Qry)
end
else
begin
select @NoRec = count(*) from #tmpSummary where StoreQty>0 and SettingID=@SettingID
set @Count=1
set @RemainTotalQty=@DebtQty
declare cur1 cursor local for
select [State], StoreQty from #tmpSummary where StoreQty>0 and SettingID=@SettingID order by StoreQty
open cur1
fetch next from cur1 into @State, @StoreQty