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
| IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE id = OBJECT_ID('tempdb..#tmp') and xtype in (N'U')) DROP TABLE #tmp
SELECT * into #tmp FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', N'Excel 12.0; IMEX=1; Database=\\s-sql-d4\OT38ElitServer\Import\ElitDistr\После_обработки_наборов.xlsx' , 'select * from [Лист1$]');
--select top 1 * from #tmp
---create table Temp_Rec (ProdIdN int,ProdID int ,PPId int, Qty numeric (21,9), PriceCC_nt numeric (21,9) , ProdIDMus int, PPIdMus int, QtyMus numeric (21,9), PriceCC_nt_Mus numeric (21,9), PriceCC_nt_Ras numeric (21,9) )
delete from Temp_Rec
DECLARE @ProdIDMA INT,@ProdIdNabor INT ,@QtyRet INT,@ProdId INT ,@PriceShop NUMERIC(21,9),@Proce NUMERIC (21,9),@PBGrID int, @ChID int,@PriceCC_nt numeric (21,9)
DECLARE CURSOR1 CURSOR --LOCAL FAST_FORWARD
FOR
SELECT top 1 ProdIDMA,ProdIdNabor,QtyRet,ProdId,PriceShop,Price,PBGrID FROM #tmp WITH (NOLOCK)
OPEN CURSOR1
FETCH NEXT FROM CURSOR1
INTO @ProdIDMA,@ProdIdNabor,@QtyRet,@ProdId,@PriceShop,@Proce,@PBGrID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @ChId = ISNULL(MAX(CHID),0) from t_Rec
SELECT @PriceCC_nt= MIN(PriceCC_nt) from t_RecD a
join t_Rec b on a.ChID=b.ChID
JOiN r_Prods c on c.ProdID=a.ProdID
WHERE StockID=20 and CompID=81 and b.ChID=@ChID and c.PBGrID=@PBGrID and Qty>@QtyRet
--insert Temp_Rec (ProdIdN,ProdID,PPId,Qty,PriceCC_nt,ProdIDMus,PPIdMus,QtyMus,PriceCC_nt_Mus,PriceCC_nt_Ras)
select top 1
@ProdIdNabor as Коднабора,
rd.ProdID,
PPID,
Qty,
PriceCC_nt,
(select top 1 rd1.ProdID from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) ProdID_Musor,
(select top 1 rd1.PPID from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) PPIDMUSOR,
(select top 1 rd1.Qty from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) QtyMusor,
(select top 1 rd1.PriceCC_nt from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) PriceCC_nt_Musor,
(@Proce-(select top 1 PriceCC_nt from
t_Rec r
join t_RecD rd on r.ChID=rd.ChID
WHERE StockID=20 and CompID=81 and Prodid=@ProdId and DocID in(select (max(docid)-1) from t_Rec r2
join t_RecD rd2 on r2.ChID=rd2.ChID
WHERE r2.StockID=20 and r2.CompID=81 and rd2.Prodid=@ProdId ) ))/2 as Rasch
from
t_Rec r
join t_RecD rd on r.ChID=rd.ChID
WHERE StockID=20 and CompID=81 and Prodid=@ProdId and DocID in(select (max(docid)-1) from t_Rec r2
join t_RecD rd2 on r2.ChID=rd2.ChID
WHERE r2.StockID=20 and r2.CompID=81 and rd2.Prodid=@ProdId )
SELECT * from Temp_Rec
FETCH NEXT FROM CURSOR1
INTO @ProdIDMA,@ProdIdNabor,@QtyRet,@ProdId,@PriceShop,@Proce,@PBGrID
END
IF EXISTS(SELECT * FROM tempdb.dbo.sysobjects WHERE id = OBJECT_ID('tempdb..#tmp') and xtype in (N'U')) DROP TABLE #tmp
SELECT * into #tmp FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', N'Excel 12.0; IMEX=1; Database=\\s-sql-d4\OT38ElitServer\Import\ElitDistr\После_обработки_наборов.xlsx' , 'select * from [Лист1$]');
--select top 1 * from #tmp
---create table Temp_Rec (ProdIdN int,ProdID int ,PPId int, Qty numeric (21,9), PriceCC_nt numeric (21,9) , ProdIDMus int, PPIdMus int, QtyMus numeric (21,9), PriceCC_nt_Mus numeric (21,9), PriceCC_nt_Ras numeric (21,9) )
delete from Temp_Rec
DECLARE @ProdIDMA INT,@ProdIdNabor INT ,@QtyRet INT,@ProdId INT ,@PriceShop NUMERIC(21,9),@Proce NUMERIC (21,9),@PBGrID int, @ChID int,@PriceCC_nt numeric (21,9),@Rasch numeric (21,9)
DECLARE CURSOR1 CURSOR --LOCAL FAST_FORWARD
FOR
SELECT top 1 ProdIDMA,ProdIdNabor,QtyRet,ProdId,PriceShop,Price,PBGrID FROM #tmp WITH (NOLOCK)
OPEN CURSOR1
FETCH NEXT FROM CURSOR1
INTO @ProdIDMA,@ProdIdNabor,@QtyRet,@ProdId,@PriceShop,@Proce,@PBGrID
WHILE @@FETCH_STATUS = 0
BEGIN
select top 1 @Rasch= (@Proce-(select top 1 PriceCC_nt from
t_Rec r
join t_RecD rd on r.ChID=rd.ChID
WHERE StockID=20 and CompID=81 and Prodid=@ProdId and DocID in(select (max(docid)-1) from t_Rec r2
join t_RecD rd2 on r2.ChID=rd2.ChID
WHERE r2.StockID=20 and r2.CompID=81 and rd2.Prodid=@ProdId ) ))/2
SELECT @ChId = ISNULL(MAX(CHID),0) from t_Rec
SELECT top 1 @PriceCC_nt= PriceCC_nt from t_RecD a
join t_Rec b on a.ChID=b.ChID
JOiN r_Prods c on c.ProdID=a.ProdID
WHERE StockID=20 and CompID=81 and b.ChID=@ChID and c.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt in (SELECT PriceCC_nt-@Rasch from t_RecD a
join t_Rec b on a.ChID=b.ChID
JOiN r_Prods c on c.ProdID=a.ProdID
WHERE StockID=20 and CompID=81 and b.ChID=@ChID and c.PBGrID=@PBGrID and Qty>@QtyRet order by PriceCC_nt-@Rasch)
--insert Temp_Rec (ProdIdN,ProdID,PPId,Qty,PriceCC_nt,ProdIDMus,PPIdMus,QtyMus,PriceCC_nt_Mus,PriceCC_nt_Ras)
select top 1
@ProdIdNabor as Коднабора,
rd.ProdID,
PPID,
Qty,
PriceCC_nt,
(select top 1 rd1.ProdID from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) ProdID_Musor,
(select top 1 rd1.PPID from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) PPIDMUSOR,
(select top 1 rd1.Qty from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) QtyMusor,
(select top 1 rd1.PriceCC_nt from
t_Rec r
join t_RecD rd1 on rd1.ChID=@ChID
JOiN r_Prods pr1 on pr1.ProdID=rd.ProdID
WHERE StockID=20 and CompID=81 and pr1.PBGrID=@PBGrID and Qty>@QtyRet and PriceCC_nt=@PriceCC_nt and rd1.ChID=@ChID ) PriceCC_nt_Musor,
(@Proce-(select top 1 PriceCC_nt from
t_Rec r
join t_RecD rd on r.ChID=rd.ChID
WHERE StockID=20 and CompID=81 and Prodid=@ProdId and DocID in(select (max(docid)-1) from t_Rec r2
join t_RecD rd2 on r2.ChID=rd2.ChID
WHERE r2.StockID=20 and r2.CompID=81 and rd2.Prodid=@ProdId ) ))/2 as Rasch
from
t_Rec r
join t_RecD rd on r.ChID=rd.ChID
WHERE StockID=20 and CompID=81 and Prodid=@ProdId and DocID in(select (max(docid)-1) from t_Rec r2
join t_RecD rd2 on r2.ChID=rd2.ChID
WHERE r2.StockID=20 and r2.CompID=81 and rd2.Prodid=@ProdId )
-- SELECT * from Temp_Rec
FETCH NEXT FROM CURSOR1
INTO @ProdIDMA,@ProdIdNabor,@QtyRet,@ProdId,@PriceShop,@Proce,@PBGrID
END
CLOSE CURSOR1
DEALLOCATE CURSOR1 |