-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmssqlsoltp.tcl
executable file
·3834 lines (3747 loc) · 183 KB
/
mssqlsoltp.tcl
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
proc build_mssqlstpcc {} {
global maxvuser suppo ntimes threadscreated _ED
upvar #0 dbdict dbdict
if {[dict exists $dbdict mssqlserver library ]} {
set library [ dict get $dbdict mssqlserver library ]
} else { set library "tdbc::odbc 1.0.6" }
if { [ llength $library ] > 1 } {
set version [ lindex $library 1 ]
set library [ lindex $library 0 ]
}
upvar #0 configmssqlserver configmssqlserver
#set variables to values in dict
setlocaltpccvars $configmssqlserver
if {![string match windows $::tcl_platform(platform)]} {
set mssqls_server $mssqls_linux_server
set mssqls_odbc_driver $mssqls_linux_odbc
set mssqls_authentication $mssqls_linux_authent
}
if {[ tk_messageBox -title "Create Schema" -icon question -message "Ready to create a $mssqls_count_ware Warehouse MS SQL Server TPROC-C schema\nin host [string toupper $mssqls_server ] in database [ string toupper $mssqls_dbase ]?" -type yesno ] == yes} {
if { $mssqls_num_vu eq 1 || $mssqls_count_ware eq 1 } {
set maxvuser 1
} else {
set maxvuser [ expr $mssqls_num_vu + 1 ]
}
set suppo 1
set ntimes 1
ed_edit_clear
set _ED(packagekeyname) "TPROC-C creation"
if { [catch {load_virtual} message]} {
puts "Failed to created thread for schema creation: $message"
return
}
.ed_mainFrame.mainwin.textFrame.left.text fastinsert end "#!/usr/local/bin/tclsh8.6
#LOAD LIBRARIES AND MODULES
set library $library
set version $version
"
.ed_mainFrame.mainwin.textFrame.left.text fastinsert end {if [catch {package require $library $version} message] { error "Failed to load $library - $message" }
if [catch {::tcl::tm::path add modules} ] { error "Failed to find modules directory" }
if [catch {package require tpcccommon} ] { error "Failed to load tpcc common functions" } else { namespace import tpcccommon::* }
proc CreateStoredProcs { odbc imdb } {
puts "CREATING TPCC STORED PROCEDURES"
if { $imdb } {
set sql(1) {CREATE PROCEDURE [dbo].[neword]
@no_w_id int,
@no_max_w_id int,
@no_d_id int,
@no_c_id int,
@no_o_ol_cnt int,
@TIMESTAMP datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@no_c_discount smallmoney,
@no_c_last char(16),
@no_c_credit char(2),
@no_d_tax smallmoney,
@no_w_tax smallmoney,
@no_d_next_o_id int,
@no_ol_supply_w_id int,
@no_ol_i_id int,
@no_ol_quantity int,
@no_o_all_local int,
@o_id int,
@no_i_name char(24),
@no_i_price smallmoney,
@no_i_data char(50),
@no_s_quantity int,
@no_ol_amount int,
@no_s_dist_01 char(24),
@no_s_dist_02 char(24),
@no_s_dist_03 char(24),
@no_s_dist_04 char(24),
@no_s_dist_05 char(24),
@no_s_dist_06 char(24),
@no_s_dist_07 char(24),
@no_s_dist_08 char(24),
@no_s_dist_09 char(24),
@no_s_dist_10 char(24),
@no_ol_dist_info char(24),
@no_s_data char(50),
@x int,
@rbk int
BEGIN TRANSACTION
BEGIN TRY
SET @no_o_all_local = 1
SELECT
@no_c_discount = c_discount,
@no_c_last = c_last,
@no_c_credit = c_credit
FROM dbo.customer
WHERE
c_w_id = @no_w_id AND
c_d_id = @no_d_id AND
c_id = @no_c_id
UPDATE dbo.district
SET
@no_d_tax = d_tax,
@o_id = d_next_o_id,
@no_d_next_o_id = d_next_o_id = district.d_next_o_id + 1
WHERE district.d_id = @no_d_id
AND district.d_w_id = @no_w_id
SET @rbk = CAST(100 * RAND() + 1 AS INT)
DECLARE
@loop_counter int
SET @loop_counter = 1
DECLARE
@loop$bound int
SET @loop$bound = @no_o_ol_cnt
WHILE @loop_counter <= @loop$bound
BEGIN
IF ((@loop_counter = @no_o_ol_cnt) AND (@rbk = 1))
SET @no_ol_i_id = 100001
ELSE
SET @no_ol_i_id = CAST(100000 * RAND() + 1 AS INT)
SET @x = CAST(100 * RAND() + 1 AS INT)
IF (@x > 1)
SET @no_ol_supply_w_id = @no_w_id
ELSE
BEGIN
SET @no_ol_supply_w_id = @no_w_id
SET @no_o_all_local = 0
WHILE ((@no_ol_supply_w_id = @no_w_id) AND (@no_max_w_id != 1))
BEGIN
SET @no_ol_supply_w_id = CAST(@no_max_w_id * RAND() + 1 AS INT)
DECLARE
@db_null_statement$2 int
END
END
SET @no_ol_quantity = CAST(10 * RAND() + 1 AS INT)
SELECT @no_i_price = item.i_price
, @no_i_name = item.i_name
, @no_i_data = item.i_data
FROM dbo.item
WHERE item.i_id = @no_ol_i_id
UPDATE dbo.stock
SET
s_quantity = s_quantity - @no_ol_quantity + CASE WHEN (s_quantity > @no_ol_quantity)
THEN 0 ELSE 91 END,
@no_s_data = s_data,
@no_ol_dist_info =
CASE @no_d_id
WHEN 1 THEN s_dist_01
WHEN 2 THEN s_dist_02
WHEN 3 THEN s_dist_03
WHEN 4 THEN s_dist_04
WHEN 5 THEN s_dist_05
WHEN 6 THEN s_dist_06
WHEN 7 THEN s_dist_07
WHEN 8 THEN s_dist_08
WHEN 9 THEN s_dist_09
WHEN 10 THEN s_dist_10
END
OUTPUT
@o_id,
@no_d_id,
@no_w_id,
@loop_counter,
@no_ol_i_id,
NULL,
(@no_ol_quantity * @no_i_price),
@no_ol_supply_w_id,
@no_ol_quantity,
CASE @no_d_id
WHEN 1 THEN inserted.s_dist_01
WHEN 2 THEN inserted.s_dist_02
WHEN 3 THEN inserted.s_dist_03
WHEN 4 THEN inserted.s_dist_04
WHEN 5 THEN inserted.s_dist_05
WHEN 6 THEN inserted.s_dist_06
WHEN 7 THEN inserted.s_dist_07
WHEN 8 THEN inserted.s_dist_08
WHEN 9 THEN inserted.s_dist_09
WHEN 10 THEN inserted.s_dist_10
END
INTO dbo.order_line
WHERE
stock.s_i_id = @no_ol_i_id AND
stock.s_w_id = @no_ol_supply_w_id
SET @loop_counter = @loop_counter + 1
END
INSERT dbo.orders( o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_ol_cnt, o_all_local)
VALUES ( @o_id, @no_d_id, @no_w_id, @no_c_id, @TIMESTAMP, @no_o_ol_cnt, @no_o_all_local)
INSERT dbo.new_order(no_o_id, no_d_id, no_w_id)
VALUES (@o_id, @no_d_id, @no_w_id)
IF (@rbk = 1)
ROLLBACK TRANSACTION
SELECT @no_w_tax = warehouse.w_tax
FROM dbo.warehouse
WHERE warehouse.w_id = @no_w_id
SELECT convert(char(8), @no_c_discount) as N'@no_c_discount', @no_c_last as N'@no_c_last', @no_c_credit
as N'@no_c_credit', convert(char(8),@no_d_tax) as N'@no_d_tax', convert(char(8),@no_w_tax)
as N'@no_w_tax', @no_d_next_o_id as N'@no_d_next_o_id'
END TRY
BEGIN CATCH
IF (error_number() in (701, 41839, 41823, 41302, 41305, 41325, 41301))
SELECT 'IMOLTPERROR',ERROR_NUMBER() AS ErrorNumber
ELSE
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(2) {CREATE PROCEDURE [dbo].[delivery]
@d_w_id int,
@d_o_carrier_id int,
@timestamp datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@d_no_o_id int,
@d_d_id int,
@d_c_id int,
@d_ol_total int
BEGIN TRANSACTION
BEGIN TRY
DECLARE
@loop_counter int
SET @loop_counter = 1
WHILE @loop_counter <= 10
BEGIN
SET @d_d_id = @loop_counter
SELECT TOP 1
@d_no_o_id = no_o_id
FROM dbo.new_order
WHERE no_w_id = @d_w_id AND
no_d_id = @d_d_id
ORDER BY no_o_id ASC
IF (@@rowcount <> 0)
BEGIN
-- claim the order for this district
DELETE dbo.new_order
WHERE no_w_id = @d_w_id AND
no_d_id = @d_d_id AND
no_o_id = @d_no_o_id
UPDATE dbo.orders
SET o_carrier_id = @d_o_carrier_id
, @d_c_id = orders.o_c_id
WHERE orders.o_id = @d_no_o_id
AND orders.o_d_id = @d_d_id
AND orders.o_w_id = @d_w_id
SET @d_ol_total = 0
UPDATE dbo.order_line
SET ol_delivery_d = @timestamp
, @d_ol_total = @d_ol_total + ol_amount
WHERE order_line.ol_o_id = @d_no_o_id
AND order_line.ol_d_id = @d_d_id
AND order_line.ol_w_id = @d_w_id
END
UPDATE dbo.customer SET c_balance = customer.c_balance + @d_ol_total
WHERE customer.c_id = @d_c_id
AND customer.c_d_id = @d_d_id
AND customer.c_w_id = @d_w_id
PRINT
'D: '
+
ISNULL(CAST(@d_d_id AS nvarchar(4000)), '')
+
'O: '
+
ISNULL(CAST(@d_no_o_id AS nvarchar(4000)), '')
+
'time '
+
ISNULL(CAST(@timestamp AS nvarchar(4000)), '')
SET @loop_counter = @loop_counter + 1
END
SELECT @d_w_id as N'@d_w_id', @d_o_carrier_id as N'@d_o_carrier_id', @timestamp as N'@TIMESTAMP'
END TRY
BEGIN CATCH
IF (error_number() in (701, 41839, 41823, 41302, 41305, 41325, 41301))
SELECT 'IMOLTPERROR',ERROR_NUMBER() AS ErrorNumber
ELSE
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(3) {CREATE PROCEDURE [dbo].[payment]
@p_w_id int,
@p_d_id int,
@p_c_w_id int,
@p_c_d_id int,
@p_c_id int,
@byname int,
@p_h_amount numeric(6,2),
@p_c_last char(16),
@TIMESTAMP datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@p_w_street_1 char(20),
@p_w_street_2 char(20),
@p_w_city char(20),
@p_w_state char(2),
@p_w_zip char(10),
@p_d_street_1 char(20),
@p_d_street_2 char(20),
@p_d_city char(20),
@p_d_state char(20),
@p_d_zip char(10),
@p_c_first char(16),
@p_c_middle char(2),
@p_c_street_1 char(20),
@p_c_street_2 char(20),
@p_c_city char(20),
@p_c_state char(20),
@p_c_zip char(9),
@p_c_phone char(16),
@p_c_since datetime2(0),
@p_c_credit char(32),
@p_c_credit_lim numeric(12,2),
@p_c_discount numeric(4,4),
@p_c_balance money,
--numeric(12,2),
@p_c_data varchar(500),
@namecnt int,
@p_d_name char(11),
@p_w_name char(11),
@p_c_new_data varchar(500),
@h_data varchar(30)
BEGIN TRY
IF (@byname = 1)
BEGIN
SELECT TOP 1
@p_c_id = c_id
FROM (
SELECT TOP 50 PERCENT c_id, c_first
FROM dbo.customer
WHERE
c_last = @p_c_last AND
c_w_id = @p_c_w_id AND
c_d_id = @p_c_d_id
ORDER BY c_first) X
ORDER BY c_first desc
END
BEGIN TRANSACTION
-- get customer info and update balances
UPDATE dbo.customer
SET
@p_c_balance = c_balance = c_balance - @p_h_amount,
c_data =
CASE
WHEN c_credit <> 'BC' THEN c_credit
ELSE LEFT(
ISNULL(CAST(@p_c_id AS char), '') + ' ' +
ISNULL(CAST(@p_c_d_id AS char), '') + ' ' +
ISNULL(CAST(@p_c_w_id AS char), '') + ' ' +
ISNULL(CAST(@p_d_id AS char), '') + ' ' +
ISNULL(CAST(@p_w_id AS char), '') + ' ' +
ISNULL(CAST(@p_h_amount AS CHAR(8)), '') + ' ' +
ISNULL(CAST(@TIMESTAMP AS char), '') + ' ' +
ISNULL(@p_w_name, '') + ' ' +
ISNULL(@p_d_name, '') + ' ' +
c_data,
500)
END,
@p_c_first = c_first,
@p_c_middle = c_middle,
@p_c_last = c_last,
@p_c_street_1 = c_street_1,
@p_c_street_2 = c_street_2,
@p_c_city = c_city,
@p_c_state = c_state,
@p_c_zip = c_zip,
@p_c_phone = c_phone,
@p_c_credit = c_credit,
@p_c_credit_lim = c_credit_lim,
@p_c_discount = c_discount,
@p_c_since = c_since
WHERE
c_id = @p_c_id AND
c_w_id = @p_c_w_id AND
c_d_id = @p_c_d_id
SET @h_data = (ISNULL(@p_w_name, '') + ' ' + ISNULL(@p_d_name, ''))
INSERT dbo.history( h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data)
VALUES ( @p_c_d_id, @p_c_w_id, @p_c_id, @p_d_id, @p_w_id, @TIMESTAMP, @p_h_amount, @h_data)
-- get district data and update year-to-date
UPDATE dbo.district
SET
d_ytd = d_ytd + @p_h_amount,
@p_d_street_1 = d_street_1,
@p_d_street_2 = d_street_2,
@p_d_city = d_city,
@p_d_state = d_state,
@p_d_zip = d_zip,
@p_d_name = d_name
WHERE
d_w_id = @p_w_id AND
d_id = @p_d_id
-- get warehouse data and update year-to-date
UPDATE dbo.warehouse
SET
w_ytd = w_ytd + @p_h_amount,
@p_w_street_1 = w_street_1,
@p_w_street_2 = w_street_2,
@p_w_city = w_city,
@p_w_state = w_state,
@p_w_zip = w_zip,
@p_w_name = w_name
WHERE
w_id = @p_w_id
SELECT @p_c_id as N'@p_c_id', @p_c_last as N'@p_c_last', @p_w_street_1 as N'@p_w_street_1'
, @p_w_street_2 as N'@p_w_street_2', @p_w_city as N'@p_w_city'
, @p_w_state as N'@p_w_state', @p_w_zip as N'@p_w_zip'
, @p_d_street_1 as N'@p_d_street_1', @p_d_street_2 as N'@p_d_street_2'
, @p_d_city as N'@p_d_city', @p_d_state as N'@p_d_state'
, @p_d_zip as N'@p_d_zip', @p_c_first as N'@p_c_first'
, @p_c_middle as N'@p_c_middle', @p_c_street_1 as N'@p_c_street_1'
, @p_c_street_2 as N'@p_c_street_2'
, @p_c_city as N'@p_c_city', @p_c_state as N'@p_c_state', @p_c_zip as N'@p_c_zip'
, @p_c_phone as N'@p_c_phone', @p_c_since as N'@p_c_since', @p_c_credit as N'@p_c_credit'
, @p_c_credit_lim as N'@p_c_credit_lim', @p_c_discount as N'@p_c_discount', @p_c_balance as
N'@p_c_balance'
, @p_c_data as N'@p_c_data'
END TRY
BEGIN CATCH
IF (error_number() in (701, 41839, 41823, 41302, 41305, 41325, 41301))
SELECT 'IMOLTPERROR',ERROR_NUMBER() AS ErrorNumber
ELSE
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(4) {CREATE PROCEDURE [dbo].[ostat]
@os_w_id int,
@os_d_id int,
@os_c_id int,
@byname int,
@os_c_last char(20)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@os_c_first char(16),
@os_c_middle char(2),
@os_c_balance money,
@os_o_id int,
@os_entdate datetime2(0),
@os_o_carrier_id int,
@os_ol_i_id
INT,
@os_ol_supply_w_id INT,
@os_ol_quantity INT,
@os_ol_amount
INT,
@os_ol_delivery_d DATE,
@namecnt int,
@i int
BEGIN TRANSACTION
BEGIN TRY
IF (@byname = 1)
BEGIN
SELECT TOP 1
@os_c_id = c_id,
@os_c_balance = c_balance,
@os_c_first = c_first,
@os_c_middle = c_middle,
@os_c_last = c_last
FROM (
SELECT TOP 50 PERCENT c_id, c_balance, c_first, c_middle, c_last
FROM dbo.customer
WHERE
c_last = @os_c_last AND
c_w_id = @os_w_id AND
c_d_id = @os_d_id
ORDER BY c_first) X
ORDER BY c_first desc
END
ELSE
BEGIN
SELECT @os_c_balance = customer.c_balance, @os_c_first = customer.c_first
, @os_c_middle = customer.c_middle, @os_c_last = customer.c_last
FROM dbo.customer
WHERE customer.c_id = @os_c_id AND customer.c_d_id = @os_d_id AND customer.c_w_id = @os_w_id
END
BEGIN
SELECT TOP (1) @os_o_id = o_id, @os_o_carrier_id = o_carrier_id, @os_entdate = o_entry_d
FROM dbo.orders
WHERE orders.o_d_id = @os_d_id
AND orders.o_w_id = @os_w_id
AND orders.o_c_id = @os_c_id
ORDER BY orders.o_id DESC
IF @@ROWCOUNT = 0
PRINT 'No orders for customer';
END
SELECT order_line.ol_i_id
, order_line.ol_supply_w_id
, order_line.ol_quantity
, order_line.ol_amount
, order_line.ol_delivery_d
FROM dbo.order_line
WHERE order_line.ol_o_id = @os_o_id
AND order_line.ol_d_id = @os_d_id
AND order_line.ol_w_id = @os_w_id
SELECT @os_c_id as N'@os_c_id', @os_c_last as N'@os_c_last', @os_c_first as N'@os_c_first', @os_c_middle
as N'@os_c_middle', @os_c_balance as N'@os_c_balance', @os_o_id as N'@os_o_id', @os_entdate as
N'@os_entdate', @os_o_carrier_id as N'@os_o_carrier_id'
END TRY
BEGIN CATCH
IF (error_number() in (701, 41839, 41823, 41302, 41305, 41325, 41301))
SELECT 'IMOLTPERROR',ERROR_NUMBER() AS ErrorNumber
ELSE
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(5) {CREATE PROCEDURE [dbo].[slev]
@st_w_id int,
@st_d_id int,
@threshold int
AS
BEGIN
DECLARE
@st_o_id_low int,
@st_o_id_high int
BEGIN TRANSACTION
BEGIN TRY
SELECT
@st_o_id_low = district.d_next_o_id - 20,
@st_o_id_high = district.d_next_o_id - 1
FROM dbo.district
WHERE district.d_w_id = @st_w_id AND district.d_id = @st_d_id
SELECT
COUNT(DISTINCT stock.s_i_id)
FROM dbo.order_line
, dbo.stock
WHERE order_line.ol_w_id = @st_w_id
AND order_line.ol_d_id = @st_d_id
AND order_line.ol_o_id BETWEEN @st_o_id_low AND @st_o_id_high
AND stock.s_w_id = order_line.ol_w_id
AND stock.s_i_id = order_line.ol_i_id
AND stock.s_quantity < @threshold
OPTION (ORDER GROUP, LOOP JOIN, MAXDOP 1);
END TRY
BEGIN CATCH
IF (error_number() in (701, 41839, 41823, 41302, 41305, 41325, 41301))
SELECT 'IMOLTPERROR',ERROR_NUMBER() AS ErrorNumber
ELSE
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
} else {
set sql(1) {CREATE PROCEDURE [dbo].[neword]
@no_w_id int,
@no_max_w_id int,
@no_d_id int,
@no_c_id int,
@no_o_ol_cnt int,
@TIMESTAMP datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@no_c_discount smallmoney,
@no_c_last char(16),
@no_c_credit char(2),
@no_d_tax smallmoney,
@no_w_tax smallmoney,
@no_d_next_o_id int,
@no_ol_supply_w_id int,
@no_ol_i_id int,
@no_ol_quantity int,
@no_o_all_local int,
@o_id int,
@no_i_name char(24),
@no_i_price smallmoney,
@no_i_data char(50),
@no_s_quantity int,
@no_ol_amount int,
@no_s_dist_01 char(24),
@no_s_dist_02 char(24),
@no_s_dist_03 char(24),
@no_s_dist_04 char(24),
@no_s_dist_05 char(24),
@no_s_dist_06 char(24),
@no_s_dist_07 char(24),
@no_s_dist_08 char(24),
@no_s_dist_09 char(24),
@no_s_dist_10 char(24),
@no_ol_dist_info char(24),
@no_s_data char(50),
@x int,
@rbk int
BEGIN TRANSACTION
BEGIN TRY
SET @no_o_all_local = 1
SELECT
@no_c_discount = c_discount,
@no_c_last = c_last,
@no_c_credit = c_credit
FROM dbo.customer
WHERE
c_w_id = @no_w_id AND
c_d_id = @no_d_id AND
c_id = @no_c_id
UPDATE dbo.district
SET
@no_d_tax = d_tax,
@o_id = d_next_o_id,
@no_d_next_o_id = d_next_o_id = district.d_next_o_id + 1
WHERE district.d_id = @no_d_id
AND district.d_w_id = @no_w_id
SET @rbk = CAST(100 * RAND() + 1 AS INT)
DECLARE
@loop_counter int
SET @loop_counter = 1
DECLARE
@loop$bound int
SET @loop$bound = @no_o_ol_cnt
WHILE @loop_counter <= @loop$bound
BEGIN
IF ((@loop_counter = @no_o_ol_cnt) AND (@rbk = 1))
SET @no_ol_i_id = 100001
ELSE
SET @no_ol_i_id = CAST(100000 * RAND() + 1 AS INT)
SET @x = CAST(100 * RAND() + 1 AS INT)
IF (@x > 1)
SET @no_ol_supply_w_id = @no_w_id
ELSE
BEGIN
SET @no_ol_supply_w_id = @no_w_id
SET @no_o_all_local = 0
WHILE ((@no_ol_supply_w_id = @no_w_id) AND (@no_max_w_id != 1))
BEGIN
SET @no_ol_supply_w_id = CAST(@no_max_w_id * RAND() + 1 AS INT)
DECLARE
@db_null_statement$2 int
END
END
SET @no_ol_quantity = CAST(10 * RAND() + 1 AS INT)
SELECT @no_i_price = item.i_price
, @no_i_name = item.i_name
, @no_i_data = item.i_data
FROM dbo.item
WHERE item.i_id = @no_ol_i_id
UPDATE dbo.stock
SET
s_quantity = s_quantity - @no_ol_quantity + CASE WHEN (s_quantity > @no_ol_quantity)
THEN 0 ELSE 91 END,
@no_s_data = s_data,
@no_ol_dist_info =
CASE @no_d_id
WHEN 1 THEN s_dist_01
WHEN 2 THEN s_dist_02
WHEN 3 THEN s_dist_03
WHEN 4 THEN s_dist_04
WHEN 5 THEN s_dist_05
WHEN 6 THEN s_dist_06
WHEN 7 THEN s_dist_07
WHEN 8 THEN s_dist_08
WHEN 9 THEN s_dist_09
WHEN 10 THEN s_dist_10
END
OUTPUT
@o_id,
@no_d_id,
@no_w_id,
@loop_counter,
@no_ol_i_id,
NULL,
(@no_ol_quantity * @no_i_price),
@no_ol_supply_w_id,
@no_ol_quantity,
CASE @no_d_id
WHEN 1 THEN inserted.s_dist_01
WHEN 2 THEN inserted.s_dist_02
WHEN 3 THEN inserted.s_dist_03
WHEN 4 THEN inserted.s_dist_04
WHEN 5 THEN inserted.s_dist_05
WHEN 6 THEN inserted.s_dist_06
WHEN 7 THEN inserted.s_dist_07
WHEN 8 THEN inserted.s_dist_08
WHEN 9 THEN inserted.s_dist_09
WHEN 10 THEN inserted.s_dist_10
END
INTO dbo.order_line
WHERE
stock.s_i_id = @no_ol_i_id AND
stock.s_w_id = @no_ol_supply_w_id
SET @loop_counter = @loop_counter + 1
END
INSERT dbo.orders( o_id, o_d_id, o_w_id, o_c_id, o_entry_d, o_ol_cnt, o_all_local)
VALUES ( @o_id, @no_d_id, @no_w_id, @no_c_id, @TIMESTAMP, @no_o_ol_cnt, @no_o_all_local)
INSERT dbo.new_order(no_o_id, no_d_id, no_w_id)
VALUES (@o_id, @no_d_id, @no_w_id)
IF (@rbk = 1)
ROLLBACK TRANSACTION
SELECT @no_w_tax = warehouse.w_tax
FROM dbo.warehouse
WHERE warehouse.w_id = @no_w_id
SELECT convert(char(8), @no_c_discount) as N'@no_c_discount', @no_c_last as N'@no_c_last', @no_c_credit
as N'@no_c_credit', convert(char(8),@no_d_tax) as N'@no_d_tax', convert(char(8),@no_w_tax)
as N'@no_w_tax', @no_d_next_o_id as N'@no_d_next_o_id'
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(2) {CREATE PROCEDURE [dbo].[delivery]
@d_w_id int,
@d_o_carrier_id int,
@timestamp datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@d_no_o_id int,
@d_d_id int,
@d_c_id int,
@d_ol_total int
BEGIN TRANSACTION
BEGIN TRY
DECLARE
@loop_counter int
SET @loop_counter = 1
WHILE @loop_counter <= 10
BEGIN
SET @d_d_id = @loop_counter
SELECT TOP 1
@d_no_o_id = no_o_id
FROM dbo.new_order WITH (serializable updlock)
WHERE no_w_id = @d_w_id AND
no_d_id = @d_d_id
ORDER BY no_o_id ASC
IF (@@rowcount <> 0)
BEGIN
-- claim the order for this district
DELETE dbo.new_order
WHERE no_w_id = @d_w_id AND
no_d_id = @d_d_id AND
no_o_id = @d_no_o_id
UPDATE dbo.orders
SET o_carrier_id = @d_o_carrier_id
, @d_c_id = orders.o_c_id
WHERE orders.o_id = @d_no_o_id
AND orders.o_d_id = @d_d_id
AND orders.o_w_id = @d_w_id
SET @d_ol_total = 0
UPDATE dbo.order_line
SET ol_delivery_d = @timestamp
, @d_ol_total = @d_ol_total + ol_amount
WHERE order_line.ol_o_id = @d_no_o_id
AND order_line.ol_d_id = @d_d_id
AND order_line.ol_w_id = @d_w_id
END
UPDATE dbo.customer SET c_balance = customer.c_balance + @d_ol_total
WHERE customer.c_id = @d_c_id
AND customer.c_d_id = @d_d_id
AND customer.c_w_id = @d_w_id
PRINT
'D: '
+
ISNULL(CAST(@d_d_id AS nvarchar(4000)), '')
+
'O: '
+
ISNULL(CAST(@d_no_o_id AS nvarchar(4000)), '')
+
'time '
+
ISNULL(CAST(@timestamp AS nvarchar(4000)), '')
SET @loop_counter = @loop_counter + 1
END
SELECT @d_w_id as N'@d_w_id', @d_o_carrier_id as N'@d_o_carrier_id', @timestamp as N'@TIMESTAMP'
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(3) {CREATE PROCEDURE [dbo].[payment]
@p_w_id int,
@p_d_id int,
@p_c_w_id int,
@p_c_d_id int,
@p_c_id int,
@byname int,
@p_h_amount numeric(6,2),
@p_c_last char(16),
@TIMESTAMP datetime2(0)
AS
BEGIN
SET ANSI_WARNINGS OFF
DECLARE
@p_w_street_1 char(20),
@p_w_street_2 char(20),
@p_w_city char(20),
@p_w_state char(2),
@p_w_zip char(10),
@p_d_street_1 char(20),
@p_d_street_2 char(20),
@p_d_city char(20),
@p_d_state char(20),
@p_d_zip char(10),
@p_c_first char(16),
@p_c_middle char(2),
@p_c_street_1 char(20),
@p_c_street_2 char(20),
@p_c_city char(20),
@p_c_state char(20),
@p_c_zip char(9),
@p_c_phone char(16),
@p_c_since datetime2(0),
@p_c_credit char(32),
@p_c_credit_lim numeric(12,2),
@p_c_discount numeric(4,4),
@p_c_balance money,
--numeric(12,2),
@p_c_data varchar(500),
@namecnt int,
@p_d_name char(11),
@p_w_name char(11),
@p_c_new_data varchar(500),
@h_data varchar(30)
BEGIN TRY
IF (@byname = 1)
BEGIN
SELECT TOP 1
@p_c_id = c_id
FROM (
SELECT TOP 50 PERCENT c_id, c_first
FROM dbo.customer WITH (repeatableread)
WHERE
c_last = @p_c_last AND
c_w_id = @p_c_w_id AND
c_d_id = @p_c_d_id
ORDER BY c_first) X
ORDER BY c_first desc
END
BEGIN TRANSACTION
-- get customer info and update balances
UPDATE dbo.customer
SET
@p_c_balance = c_balance = c_balance - @p_h_amount,
c_data =
CASE
WHEN c_credit <> 'BC' THEN c_credit
ELSE LEFT(
ISNULL(CAST(@p_c_id AS char), '') + ' ' +
ISNULL(CAST(@p_c_d_id AS char), '') + ' ' +
ISNULL(CAST(@p_c_w_id AS char), '') + ' ' +
ISNULL(CAST(@p_d_id AS char), '') + ' ' +
ISNULL(CAST(@p_w_id AS char), '') + ' ' +
ISNULL(CAST(@p_h_amount AS CHAR(8)), '') + ' ' +
ISNULL(CAST(@TIMESTAMP AS char), '') + ' ' +
ISNULL(@p_w_name, '') + ' ' +
ISNULL(@p_d_name, '') + ' ' +
c_data,
500)
END,
@p_c_first = c_first,
@p_c_middle = c_middle,
@p_c_last = c_last,
@p_c_street_1 = c_street_1,
@p_c_street_2 = c_street_2,
@p_c_city = c_city,
@p_c_state = c_state,
@p_c_zip = c_zip,
@p_c_phone = c_phone,
@p_c_credit = c_credit,
@p_c_credit_lim = c_credit_lim,
@p_c_discount = c_discount,
@p_c_since = c_since
WHERE
c_id = @p_c_id AND
c_w_id = @p_c_w_id AND
c_d_id = @p_c_d_id
SET @h_data = (ISNULL(@p_w_name, '') + ' ' + ISNULL(@p_d_name, ''))
INSERT dbo.history( h_c_d_id, h_c_w_id, h_c_id, h_d_id, h_w_id, h_date, h_amount, h_data)
VALUES ( @p_c_d_id, @p_c_w_id, @p_c_id, @p_d_id, @p_w_id, @TIMESTAMP, @p_h_amount, @h_data)
-- get district data and update year-to-date
UPDATE dbo.district
SET
d_ytd = d_ytd + @p_h_amount,
@p_d_street_1 = d_street_1,
@p_d_street_2 = d_street_2,
@p_d_city = d_city,
@p_d_state = d_state,
@p_d_zip = d_zip,
@p_d_name = d_name
WHERE
d_w_id = @p_w_id AND
d_id = @p_d_id
-- get warehouse data and update year-to-date
UPDATE dbo.warehouse
SET
w_ytd = w_ytd + @p_h_amount,
@p_w_street_1 = w_street_1,
@p_w_street_2 = w_street_2,
@p_w_city = w_city,
@p_w_state = w_state,
@p_w_zip = w_zip,
@p_w_name = w_name
WHERE
w_id = @p_w_id
SELECT @p_c_id as N'@p_c_id', @p_c_last as N'@p_c_last', @p_w_street_1 as N'@p_w_street_1'
, @p_w_street_2 as N'@p_w_street_2', @p_w_city as N'@p_w_city'
, @p_w_state as N'@p_w_state', @p_w_zip as N'@p_w_zip'
, @p_d_street_1 as N'@p_d_street_1', @p_d_street_2 as N'@p_d_street_2'
, @p_d_city as N'@p_d_city', @p_d_state as N'@p_d_state'
, @p_d_zip as N'@p_d_zip', @p_c_first as N'@p_c_first'
, @p_c_middle as N'@p_c_middle', @p_c_street_1 as N'@p_c_street_1'
, @p_c_street_2 as N'@p_c_street_2'
, @p_c_city as N'@p_c_city', @p_c_state as N'@p_c_state', @p_c_zip as N'@p_c_zip'
, @p_c_phone as N'@p_c_phone', @p_c_since as N'@p_c_since', @p_c_credit as N'@p_c_credit'
, @p_c_credit_lim as N'@p_c_credit_lim', @p_c_discount as N'@p_c_discount', @p_c_balance as
N'@p_c_balance'
, @p_c_data as N'@p_c_data'
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
COMMIT TRANSACTION;
END}
set sql(4) {CREATE PROCEDURE [dbo].[ostat]
@os_w_id int,
@os_d_id int,
@os_c_id int,
@byname int,
@os_c_last char(20)
AS
BEGIN