挂sqlserver计划,系统自动分配拣货任务
Posted Evan_Zhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了挂sqlserver计划,系统自动分配拣货任务相关的知识,希望对你有一定的参考价值。
1 USE [P2WMS_WH43] 2 GO 3 /****** Object: StoredProcedure [dbo].[sp_fru_CalcAllocatePickData] Script Date: 05/04/2018 11:41:56 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 -- ============================================= 9 -- Author: Evan 10 -- Create date: 2018年4月17日 14点53分 11 -- Description: 自动分配待拣数据,寄宿在SQL server的job任务[FRU自动按照库位分配拣货任务],每5分钟跑一次 12 -- Logic: 1:汇总待分配的DN(DN,PartNO,Location,BOL),同一个DN可能会对应多个不同库位。#temp_DNList 13 -- 2: 汇总需要分配的库位(distinct)。#temp_AllocationLocation 14 -- 3:遍历#temp_AllocationLocation.LocationCode,匹配此LocationCode对应表FRU_PickLocationNew.Account手上DN量最少的账户, 15 -- 将此次循环下的LocationCode的DN(包括这个DN下对应的另外的库位)全部给这个人,最后将已分配的数据从#temp_DNList删除。 16 -- ============================================= 17 CreatePROCEDURE [dbo].[sp_fru_CalcAllocatePickData] 18 AS 19 BEGIN 20 ----------------------------------------找出未拣货的DN 21 IF EXISTS ( SELECT * 22 FROM tempdb..sysobjects 23 WHERE id = OBJECT_ID(‘tempdb..#temp_DNList‘) ) 24 BEGIN 25 DROP TABLE #temp_box 26 END 27 CREATE TABLE #temp_DNList--汇总未分配账户未拣货的DN 28 ( 29 DN NVARCHAR(MAX) , 30 PartNO NVARCHAR(250) , 31 LocationCode NVARCHAR(250) , 32 BOL NVARCHAR(250) 33 ) 34 INSERT INTO #temp_DNList 35 ( DN , 36 PartNO , 37 LocationCode , 38 BOL 39 ) 40 SELECT wo.UserDefineAttribute01 , 41 wop.PartNO , 42 wopp.LocationCode , 43 --SUM(CAST( wopp.PickEAQty AS DECIMAL(18,0))) PickEAQty, 44 wo.UserDefineAttribute02 45 FROM dbo.WMS_Outbound wo 46 INNER JOIN dbo.WMS_OutboundPart wop ON wop.OutboundID = wo.OutboundID 47 INNER JOIN dbo.WMS_OutboundPick wopp ON wopp.OutboundPartID = wop.OutboundPartID 48 LEFT JOIN dbo.WMS_FRU_DNAllocationToAccount dta ON dta.DN = wo.UserDefineAttribute01 49 WHERE ISNULL(wopp.TargetLocationCode, ‘‘) = ‘‘ 50 AND ISNULL(wo.UserDefineAttribute02, ‘‘) <> ‘‘ 51 AND ISNULL(wop.AllocationEAQty, 0) > 0 52 AND ISNULL(dta.DN, ‘‘) = ‘‘ 53 GROUP BY wo.UserDefineAttribute01 , 54 wop.PartNO , 55 wopp.LocationCode , 56 wo.UserDefineAttribute02 57 ORDER BY wopp.LocationCode 58 59 SELECT * 60 FROM #temp_DNList 61 ORDER BY LocationCode; 62 ----------------------------------------------------------计算启用的账户手上的已分配的DN数,一个DN对应一个料 63 WITH a AS ( SELECT fpln.Account , 64 ISNULL(COUNT(fdt.DN), 0) OrderNum 65 FROM ( SELECT DISTINCT 66 Account 67 FROM dbo.FRU_PickLocationNew 68 WHERE IsUse = 1 69 ) fpln 70 LEFT JOIN WMS_FRU_DNAllocationToAccount fdt ON fdt.Picker = fpln.Account 71 INNER JOIN dbo.WMS_Outbound wo ON wo.UserDefineAttribute01 = fdt.DN 72 AND WO.PickState <> 2 73 AND wo.ShipmentState <> 3 74 GROUP BY fpln.Account 75 ), 76 b AS ( SELECT DISTINCT 77 Account 78 FROM dbo.FRU_PickLocationNew 79 WHERE IsUse = 1 80 ) 81 SELECT b.Account , 82 CASE WHEN ISNULL(a.Account, ‘‘) <> ‘‘ THEN a.OrderNum 83 ELSE 0 84 END OrderNum 85 FROM b 86 LEFT JOIN a ON a.Account = b.Account 87 ---------------------------------------------------------汇总本次需要系统自动分配库位,按照库位分配至账号,优先挑手上DN量最少的账户 88 IF EXISTS ( SELECT * 89 FROM tempdb..sysobjects 90 WHERE id = OBJECT_ID(‘tempdb..#temp_AllocationLocation‘) ) 91 BEGIN 92 DROP TABLE #temp_AllocationLocation 93 END 94 CREATE TABLE #temp_AllocationLocation 95 ( 96 LocationCode NVARCHAR(MAX) , 97 RN INT 98 ) 99 INSERT INTO #temp_AllocationLocation 100 ( LocationCode , 101 RN 102 ) 103 SELECT sss.LocationCode , 104 ROW_NUMBER() OVER ( ORDER BY sss.LocationCode ) RN 105 FROM ( SELECT DISTINCT 106 LocationCode 107 FROM #temp_DNList 108 ) sss 109 SELECT * 110 FROM #temp_AllocationLocation 111 ORDER BY LocationCode; 112 DECLARE @i INT= 1; 113 DECLARE @de_Location NVARCHAR(250);--当前遍历处理的库位 114 DECLARE @de_Account NVARCHAR(250);--当前要分配任务至的账户,挑选目前手上DN量最少的账户 115 WHILE @i <= ( SELECT MAX(RN) 116 FROM #temp_AllocationLocation 117 ) 118 BEGIN 119 120 SELECT @de_Location = LocationCode 121 FROM #temp_AllocationLocation 122 WHERE RN = @i; 123 IF EXISTS ( SELECT TOP 1 124 1 125 FROM #temp_DNList 126 WHERE LocationCode = @de_Location ) 127 BEGIN 128 WITH a AS ( SELECT fpln.Account , 129 ISNULL(COUNT(fdt.DN), 0) OrderNum 130 FROM ( SELECT DISTINCT 131 Account 132 FROM dbo.FRU_PickLocationNew 133 WHERE IsUse = 1 134 ) fpln 135 LEFT JOIN WMS_FRU_DNAllocationToAccount fdt ON fdt.Picker = fpln.Account 136 INNER JOIN dbo.WMS_Outbound wo ON wo.UserDefineAttribute01 = fdt.DN 137 AND WO.PickState <> 2 138 AND wo.ShipmentState <> 3 139 GROUP BY fpln.Account 140 ), 141 b AS ( SELECT DISTINCT 142 Account 143 FROM dbo.FRU_PickLocationNew 144 WHERE IsUse = 1 145 AND Location = SUBSTRING(@de_Location, 146 1, 2) 147 ) 148 SELECT TOP 1 149 @de_Account = sss.Account 150 FROM ( SELECT b.Account , 151 CASE WHEN ISNULL(a.Account, ‘‘) <> ‘‘ 152 THEN a.OrderNum 153 ELSE 0 154 END OrderNum 155 FROM b 156 LEFT JOIN a ON a.Account = b.Account 157 ) sss 158 ORDER BY sss.OrderNum , 159 sss.Account 160 --此次遍历的库位未在表FRU_PickLocationNew维护账户,因此未获取到可分配的账户 161 IF ( ISNULL(@de_Account, ‘‘) <> ‘‘ ) 162 BEGIN 163 --发现该Location下存在DN要去不同库位上拣货,那么该DN全部分配给这个人 164 DECLARE @IsRepeatDN INT= 0;--标识,该Location下是否存在DN要去不同库位拣货,1表示存在这些DN 165 IF EXISTS ( SELECT * 166 FROM tempdb..sysobjects 167 WHERE id = OBJECT_ID(‘tempdb..#temp_RepeatDN‘) ) 168 BEGIN 169 DROP TABLE #temp_RepeatDN 170 END 171 CREATE TABLE #temp_RepeatDN ( DN NVARCHAR(MAX) ) 172 IF EXISTS ( SELECT DN 173 FROM #temp_DNList 174 WHERE dn IN ( 175 SELECT dn 176 FROM #temp_DNList 177 WHERE LocationCode = @de_Location ) 178 GROUP BY DN 179 HAVING COUNT(DN) > 1 ) 180 BEGIN 181 INSERT INTO #temp_RepeatDN 182 ( DN 183 ) 184 SELECT DISTINCT 185 SSS.DN 186 FROM ( SELECT 187 DN 188 FROM 189 #temp_DNList 190 WHERE 191 dn IN ( 192 SELECT 193 dn 194 FROM 195 #temp_DNList 196 WHERE 197 LocationCode = @de_Location ) 198 GROUP BY DN 199 HAVING 200 COUNT(DN) > 1 201 ) SSS 202 SELECT @IsRepeatDN = 1; 203 END 204 --------------------------- 205 IF ( @IsRepeatDN <> 1 ) 206 BEGIN 207 INSERT INTO dbo.WMS_FRU_DNAllocationToAccount 208 ( DN , 209 LocationCode , 210 BOL , 211 Picker , 212 CreateBy , 213 CreateDate 214 ) 215 SELECT td.DN , 216 td.LocationCode , 217 td.BOL , 218 @de_Account , 219 ‘Evan‘ , 220 GETDATE() 221 FROM #temp_DNList td 222 WHERE td.LocationCode = @de_Location 223 END 224 ELSE 225 BEGIN 226 INSERT INTO dbo.WMS_FRU_DNAllocationToAccount 227 ( DN , 228 LocationCode , 229 BOL , 230 Picker , 231 CreateBy , 232 CreateDate 233 ) 234 SELECT td.DN , 235 td.LocationCode , 236 td.BOL , 237 @de_Account , 238 ‘Evan‘ , 239 GETDATE() 240 FROM #temp_DNList td 241 WHERE td.LocationCode = @de_Location 242 AND TD.DN NOT IN ( 243 SELECT 244 dn 245 FROM #temp_RepeatDN ) 246 247 INSERT INTO dbo.WMS_FRU_DNAllocationToAccount 248 ( DN , 249 LocationCode , 250 BOL , 251 Picker , 252 CreateBy , 253 CreateDate 254 ) 255 SELECT td.DN , 256 td.LocationCode , 257 td.BOL , 258 @de_Account , 259 ‘Evan‘ , 260 GETDATE() 261 FROM #temp_DNList td 262 WHERE TD.DN IN ( SELECT 263 dn 264 FROM 265 #temp_RepeatDN ) 266 END 267 268 DELETE FROM #temp_DNList 269 WHERE LocationCode = @de_Location; 270 DELETE FROM #temp_DNList 271 WHERE dn IN ( SELECT dn 272 FROM #temp_RepeatDN ); 273 DROP TABLE #temp_RepeatDN; 274 END 275 END 276 SET @i = @i + 1; 277 278 279 END 280 SELECT * 281 FROM #temp_DNList; 282 ----------------------------------------------------- 283 284 END
以上是关于挂sqlserver计划,系统自动分配拣货任务的主要内容,如果未能解决你的问题,请参考以下文章