Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.16 KB | None | 0 0
  1. USE [BELBaseline]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[sp_DMS_BaselineDaily] Script Date: 10/22/2019 2:20:51 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author: Dinh Hoang Lam
  10. -- Create date: 2015/04/02
  11. -- Description:
  12. -- =============================================
  13. -- EXEC sp_DMS_BaselineDaily 3, '2018/04/01', '2018/04/26', null
  14. ALTER PROCEDURE [dbo].[sp_DMS_BaselineDaily]
  15. @CompanyID INT
  16. , @FromDate DATETIME
  17. , @ToDate DATETIME
  18. , @DistributorID VARCHAR(MAX)
  19. AS
  20. BEGIN
  21. SET NOCOUNT ON
  22.  
  23. DECLARE @_CompanyID INT = @CompanyID
  24. DECLARE @_FromDate DATETIME = @FromDate
  25. DECLARE @_ToDate DATETIME = @ToDate
  26. DECLARE @_DistributorID VARCHAR(MAX) = NULLIF(LTRIM(RTRIM(@DistributorID)), '')
  27. DECLARE @_Month INT = MONTH(@_FromDate)
  28. DECLARE @_Year INT = YEAR(@_FromDate)
  29. DECLARE @_MonthID CHAR(6) = RIGHT('0' + CONVERT(VARCHAR(2), @_Month), 2) + CONVERT(VARCHAR(4), @_Year)
  30. DECLARE @_FirstDayOfMonth DATETIME = CONVERT(DATETIME, CONVERT(VARCHAR(4), @_Year) + '/' + CONVERT(VARCHAR(2), @_Month) + '/1')
  31. DECLARE @_LastDayOfMonth DATETIME = DATEADD(dd, -1, DATEADD(mm, 1, @_FirstDayOfMonth))
  32. DECLARE @_FirstDayOfPreMonth DATETIME = DATEADD(mm, -1, @_FirstDayOfMonth)
  33. DECLARE @_Weekend CHAR(3)
  34. DECLARE @_LeadDate INT
  35. DECLARE @_ClosedDate DATETIME
  36. DECLARE @_Date DATETIME
  37. DECLARE @_Holiday DATETIME
  38. DECLARE @_CheckDate datetime = getdate()
  39.  
  40. SELECT * INTO #tmpDistributor FROM dbo.fn_DMS_ConvertIDListToTable(@_DistributorID)
  41. IF NOT EXISTS (SELECT TOP 1 * FROM #tmpDistributor)
  42. INSERT INTO #tmpDistributor
  43. SELECT DistributorID FROM BELDMS..DMSViewDistributor WHERE CompanyID = @_CompanyID
  44.  
  45. SELECT @_Weekend = (SELECT Weekend FROM BELDMS..DMSSalesCalendar WHERE CompanyID = @_CompanyID AND YEAR = YEAR(@_ToDate))
  46. SELECT @_LeadDate = (SELECT LeadDate FROM BELDMS..DMSBaselineConfig WHERE CompanyID = @_CompanyID)
  47.  
  48. IF @_LeadDate IS NOT NULL AND UPPER(SUBSTRING(DATENAME(dw, @_ToDate), 1, 3)) != @_Weekend
  49. BEGIN
  50. ---- Chốt thời gian đóng ngày
  51. ---- Xóa thời gian đóng ngày trước đó
  52. DELETE FROM BELDMS..DMSBaselineClosedDate
  53. WHERE
  54. CompanyID = @_CompanyID
  55. AND EXISTS (SELECT * FROM #tmpDistributor WHERE ID = BranchID)
  56.  
  57. SET @_ClosedDate = DATEADD(dd, @_LeadDate * -1, @_ToDate)
  58. SET @_Date = DATEADD(dd, -1, @_ToDate)
  59.  
  60. WHILE @_Date > @_ClosedDate
  61. BEGIN
  62. ---- Neu ngay lead date roi vao chu nhat thi lui ve 1 ngay
  63. IF UPPER(SUBSTRING(DATENAME(dw, @_Date), 1, 3)) = @_Weekend
  64. BEGIN
  65. SET @_ClosedDate = DATEADD(dd, -1, @_ClosedDate)
  66. SET @_Date = DATEADD(dd, -1, @_Date)
  67. END
  68.  
  69. SET @_Holiday = (SELECT TOP 1 FromDate FROM BELDMS..DMSHoliday WHERE CompanyID = @_CompanyID AND YEAR = YEAR(@_Date) AND @_Date BETWEEN FromDate AND ToDate)
  70. IF @_Holiday IS NOT NULL
  71. BEGIN
  72. SET @_ClosedDate = DATEADD(dd, -1 * DATEDIFF(dd, @_Holiday, @_Date) - 1, @_ClosedDate)
  73. SET @_Date = @_Holiday
  74. END
  75.  
  76. SET @_Date = DATEADD(dd, -1, @_Date)
  77. END
  78.  
  79. ---- Thêm mới thời gian đóng ngày để chặn user nhập giao dịch
  80. INSERT INTO BELDMS..DMSBaselineClosedDate
  81. (
  82. CompanyID
  83. , BranchID
  84. , ClosedDate
  85. , CreatedByID
  86. , CreatedByScreenID
  87. , CreatedDateTime
  88. , LastModifiedByID
  89. , LastModifiedByScreenID
  90. , LastModifiedDateTime
  91. )
  92. SELECT
  93. @_CompanyID
  94. , ID
  95. , @_ClosedDate
  96. , NULL
  97. , 'BASELINE'
  98. , GETDATE()
  99. , NULL
  100. , 'BASELINE'
  101. , GETDATE()
  102. FROM
  103. #tmpDistributor
  104. END
  105.  
  106. ---- Baseline MDM
  107. EXEC dbo.sp_DMS_BaselineMDM @_CompanyID, @_ToDate
  108.  
  109.  
  110. ---- Baseline Transaction Data
  111. BEGIN
  112.  
  113. DECLARE @StartTime datetime , @EndTime datetime;
  114. DECLARE @StartRowCount FLOAT , @EndRowCount FLOAT , @RowCountNumber FLOAT;
  115. DECLARE @Message NVARCHAR(MAX) , @Parameter nvarchar(MAX);
  116. SET @Message='';
  117. DECLARE @Error NVARCHAR(MAX);
  118.  
  119. BEGIN try
  120. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_LastDayOfMonth=' + CONVERT(nvarchar(20),@_LastDayOfMonth) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  121. SET @StartTime = getdate();
  122. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLMCP)
  123. EXEC dbo.sp_DMS_Baseline_MCP @_CompanyID, @_FromDate, @_LastDayOfMonth, @_DistributorID
  124. SET @EndTime = GETDATE();
  125. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLMCP)
  126. SET @RowCountNumber = @EndRowCount - @StartRowCount
  127. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_MCP' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  128. END try
  129. BEGIN catch
  130. IF(ERROR_MESSAGE() IS NOT NULL)
  131. BEGIN
  132. SET @Error=ERROR_MESSAGE();
  133. SET @Message= @Message+' - store sp_DMS_Baseline_MCP failed: ' +@Error + '<br/>';
  134. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_MCP' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  135. END
  136. END catch
  137.  
  138. BEGIN try
  139. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  140. SET @StartTime = getdate();
  141. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLRawSales)
  142. EXEC dbo.sp_DMS_Baseline_RawSales @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  143. SET @EndTime = GETDATE();
  144. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLRawSales)
  145. SET @RowCountNumber = @EndRowCount - @StartRowCount
  146. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_RawSales' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  147. END try
  148. BEGIN catch
  149. IF(ERROR_MESSAGE() IS NOT NULL)
  150. BEGIN
  151. SET @Error=ERROR_MESSAGE();
  152. SET @Message= @Message+' - store sp_DMS_Baseline_RawSales failed: ' +@Error + '<br/>';
  153. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_RawSales' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  154. END
  155. END catch
  156.  
  157. -- EXEC dbo.sp_DMS_Baseline_IncompleteRawSales @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  158.  
  159. BEGIN try
  160. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  161. SET @StartTime = getdate();
  162. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLSalesIn)
  163. EXEC dbo.sp_DMS_Baseline_SalesIn @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  164. SET @EndTime = GETDATE();
  165. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLSalesIn)
  166. SET @RowCountNumber = @EndRowCount - @StartRowCount
  167. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_SalesIn' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  168. END try
  169. BEGIN catch
  170. IF(ERROR_MESSAGE() IS NOT NULL)
  171. BEGIN
  172. SET @Error=ERROR_MESSAGE();
  173. SET @Message= @Message+' - store sp_DMS_Baseline_SalesIn failed: ' +@Error + '<br/>';
  174. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_SalesIn' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  175. END
  176. END catch
  177.  
  178. BEGIN try
  179. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  180. SET @StartTime = getdate();
  181. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLSalesInNoRequisition)
  182. EXEC dbo.sp_DMS_Baseline_SalesInNoRequisition @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  183. SET @EndTime = GETDATE();
  184. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLSalesInNoRequisition)
  185. SET @RowCountNumber = @EndRowCount - @StartRowCount
  186. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_SalesInNoRequisition' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  187. END try
  188. BEGIN catch
  189. IF(ERROR_MESSAGE() IS NOT NULL)
  190. BEGIN
  191. SET @Error=ERROR_MESSAGE();
  192. SET @Message= @Message+' - store sp_DMS_Baseline_SalesInNoRequisition failed: ' +@Error + '<br/>';
  193. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_SalesInNoRequisition' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  194. END
  195. END catch
  196.  
  197. BEGIN try
  198. SET @Parameter = '@_CompanyID = 3,@_FirstDayOfMonth=' + CONVERT(nvarchar(20),@_FirstDayOfMonth) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  199. SET @StartTime = getdate();
  200. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLPromotionDetail)
  201. EXEC dbo.sp_DMS_Baseline_PromotionDetail @_CompanyID, @_FirstDayOfMonth, @_ToDate, @_DistributorID
  202. SET @EndTime = GETDATE();
  203. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLPromotionDetail)
  204. SET @RowCountNumber = @EndRowCount - @StartRowCount
  205. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_PromotionDetail' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  206. END try
  207. BEGIN catch
  208. IF(ERROR_MESSAGE() IS NOT NULL)
  209. BEGIN
  210. SET @Error=ERROR_MESSAGE();
  211. SET @Message= @Message+' - store sp_DMS_Baseline_PromotionDetail failed: ' +@Error + '<br/>';
  212. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_PromotionDetail' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  213. END
  214. END catch
  215.  
  216. BEGIN try
  217. SET @Parameter = '@_CompanyID = 3,@_FirstDayOfMonth=' + CONVERT(nvarchar(20),@_FirstDayOfMonth) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  218. SET @StartTime = getdate();
  219. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLPromotionDetailFull)
  220. EXEC dbo.sp_DMS_Baseline_PromotionDetailFull @_CompanyID, @_FirstDayOfMonth, @_ToDate, @_DistributorID
  221. SET @EndTime = GETDATE();
  222. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLPromotionDetailFull)
  223. SET @RowCountNumber = @EndRowCount - @StartRowCount
  224. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_PromotionDetailFull' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  225. END try
  226. BEGIN catch
  227. IF(ERROR_MESSAGE() IS NOT NULL)
  228. BEGIN
  229. SET @Error=ERROR_MESSAGE();
  230. SET @Message= @Message+' - store sp_DMS_Baseline_PromotionDetailFull failed: ' +@Error + '<br/>';
  231. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_PromotionDetailFull' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  232. END
  233. END catch
  234.  
  235.  
  236. BEGIN try
  237. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  238. SET @StartTime = getdate();
  239. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLAllocationDetail)
  240. EXEC dbo.sp_DMS_Baseline_AllocationDetail @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  241. SET @EndTime = GETDATE();
  242. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLAllocationDetail)
  243. SET @RowCountNumber = @EndRowCount - @StartRowCount
  244. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_AllocationDetail' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  245. END try
  246. BEGIN catch
  247. IF(ERROR_MESSAGE() IS NOT NULL)
  248. BEGIN
  249. SET @Error=ERROR_MESSAGE();
  250. SET @Message= @Message+' - store sp_DMS_Baseline_AllocationDetail failed: ' +@Error + '<br/>';
  251. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_AllocationDetail' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  252. END
  253. END catch
  254.  
  255. BEGIN try
  256. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  257. SET @StartTime = getdate();
  258. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLEvaluation)
  259. EXEC dbo.sp_DMS_Baseline_Evaluation @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  260. SET @EndTime = GETDATE();
  261. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLEvaluation)
  262. SET @RowCountNumber = @EndRowCount - @StartRowCount
  263. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_Evaluation' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  264. END try
  265. BEGIN catch
  266. IF(ERROR_MESSAGE() IS NOT NULL)
  267. BEGIN
  268. SET @Error=ERROR_MESSAGE();
  269. SET @Message= @Message+' - store sp_DMS_Baseline_Evaluation failed: ' +@Error + '<br/>';
  270. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_Evaluation' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  271. END
  272. END catch
  273.  
  274. BEGIN try
  275. SET @Parameter = '@_CompanyID = 3,@_FirstDayOfMonth=' + CONVERT(nvarchar(20),@_FirstDayOfMonth) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  276. SET @StartTime = getdate();
  277. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLInventoryStock)
  278. EXEC dbo.sp_DMS_Baseline_InventoryStock @_CompanyID, @_FirstDayOfMonth, @_ToDate, @_DistributorID
  279. SET @EndTime = GETDATE();
  280. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLInventoryStock)
  281. SET @RowCountNumber = @EndRowCount - @StartRowCount
  282. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_InventoryStock' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  283. END try
  284. BEGIN catch
  285. IF(ERROR_MESSAGE() IS NOT NULL)
  286. BEGIN
  287. SET @Error=ERROR_MESSAGE();
  288. SET @Message= @Message+' - store sp_DMS_Baseline_InventoryStock failed: ' +@Error + '<br/>';
  289. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_InventoryStock' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  290. END
  291. END catch
  292.  
  293.  
  294. BEGIN try
  295. SET @Parameter = '@_CompanyID = 3,@_FirstDayOfMonth=' + CONVERT(nvarchar(20),@_FirstDayOfMonth) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  296. SET @StartTime = getdate();
  297. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLInventoryStockByOrderDate)
  298. EXEC dbo.sp_DMS_Baseline_InventoryStockByOrderDate @_CompanyID, @_FirstDayOfMonth, @_ToDate, @_DistributorID
  299. SET @EndTime = GETDATE();
  300. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLInventoryStockByOrderDate)
  301. SET @RowCountNumber = @EndRowCount - @StartRowCount
  302. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_InventoryStockByOrderDate' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  303. END try
  304. BEGIN catch
  305. IF(ERROR_MESSAGE() IS NOT NULL)
  306. BEGIN
  307. SET @Error=ERROR_MESSAGE();
  308. SET @Message= @Message+' - store sp_DMS_Baseline_InventoryStockByOrderDate failed: ' +@Error + '<br/>';
  309. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_InventoryStockByOrderDate' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  310. END
  311. END catch
  312.  
  313. BEGIN try
  314. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  315. SET @StartTime = getdate();
  316. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLSales)
  317. EXEC dbo.sp_DMS_Baseline_Sales @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  318. SET @EndTime = GETDATE();
  319. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLSales)
  320. SET @RowCountNumber = @EndRowCount - @StartRowCount
  321. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_Sales' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  322. END try
  323. BEGIN catch
  324. IF(ERROR_MESSAGE() IS NOT NULL)
  325. BEGIN
  326. SET @Error=ERROR_MESSAGE();
  327. SET @Message= @Message+' - store sp_DMS_Baseline_Sales failed: ' +@Error + '<br/>';
  328. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_Sales' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  329. END
  330. END catch
  331.  
  332.  
  333. BEGIN try
  334. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  335. SET @StartTime = getdate();
  336. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLRouteSales)
  337. EXEC dbo.sp_DMS_Baseline_RouteSales @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  338. SET @EndTime = GETDATE();
  339. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLRouteSales)
  340. SET @RowCountNumber = @EndRowCount - @StartRowCount
  341. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_RouteSales' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  342. END try
  343. BEGIN catch
  344. IF(ERROR_MESSAGE() IS NOT NULL)
  345. BEGIN
  346. SET @Error=ERROR_MESSAGE();
  347. SET @Message= @Message+' - store sp_DMS_Baseline_RouteSales failed: ' +@Error + '<br/>';
  348. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_RouteSales' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  349. END
  350. END catch
  351.  
  352.  
  353. BEGIN try
  354. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  355. SET @StartTime = getdate();
  356. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLASO)
  357. EXEC dbo.sp_DMS_Baseline_ASO @_CompanyID, @_FromDate, @_ToDate, @_DistributorID
  358. SET @EndTime = GETDATE();
  359. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLASO)
  360. SET @RowCountNumber = @EndRowCount - @StartRowCount
  361. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_ASO' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  362. END try
  363. BEGIN catch
  364. IF(ERROR_MESSAGE() IS NOT NULL)
  365. BEGIN
  366. SET @Error=ERROR_MESSAGE();
  367. SET @Message= @Message+' - store sp_DMS_Baseline_ASO failed: ' +@Error + '<br/>';
  368. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_ASO' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  369. END
  370. END catch
  371.  
  372. BEGIN try
  373. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FirstDayOfMonth) + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  374. SET @StartTime = getdate();
  375. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLProgramTracking)
  376. EXEC dbo.sp_DMS_Baseline_ProgramTracking @_CompanyID, @_FirstDayOfMonth, @_ToDate, @_DistributorID
  377. SET @EndTime = GETDATE();
  378. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLProgramTracking)
  379. SET @RowCountNumber = @EndRowCount - @StartRowCount
  380. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_ProgramTracking' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  381. END try
  382. BEGIN catch
  383. IF(ERROR_MESSAGE() IS NOT NULL)
  384. BEGIN
  385. SET @Error=ERROR_MESSAGE();
  386. SET @Message= @Message+' - store sp_DMS_Baseline_ProgramTracking failed: ' +@Error + '<br/>';
  387. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_ProgramTracking' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  388. END
  389. END catch
  390.  
  391.  
  392. BEGIN try
  393. SET @Parameter = '@_CompanyID = 3' + ',@_ToDate=' + CONVERT(nvarchar(20),@_ToDate)
  394. SET @StartTime = getdate();
  395. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLKPI)
  396. EXEC dbo.sp_DMS_Baseline_KPI @_CompanyID, @_ToDate
  397. SET @EndTime = GETDATE();
  398. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLKPI)
  399. SET @RowCountNumber = @EndRowCount - @StartRowCount
  400. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_KPI' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  401. END try
  402. BEGIN catch
  403. IF(ERROR_MESSAGE() IS NOT NULL)
  404. BEGIN
  405. SET @Error=ERROR_MESSAGE();
  406. SET @Message= @Message+' - store sp_DMS_Baseline_KPI failed: ' +@Error + '<br/>';
  407. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_KPI' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  408. END
  409. END catch
  410.  
  411.  
  412. BEGIN try
  413. SET @Parameter = '@_CompanyID = 3,@_Month=' + CONVERT(nvarchar(20),@_Month) + ',@_Year=' + CONVERT(nvarchar(20),@_Year) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  414. SET @StartTime = getdate();
  415. SET @StartRowCount = (SELECT COUNT(1) FROM BELDMS..DMSDistributorAVGSalesOut3LM)
  416. EXEC dbo.sp_DMS_Baseline_DistributorSellInTarget @_CompanyID, @_Month, @_Year, @_DistributorID
  417. SET @EndTime = GETDATE();
  418. SET @EndRowCount = (SELECT COUNT(1) FROM BELDMS..DMSDistributorAVGSalesOut3LM)
  419. SET @RowCountNumber = @EndRowCount - @StartRowCount
  420. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSellInTarget' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  421. END try
  422. BEGIN catch
  423. IF(ERROR_MESSAGE() IS NOT NULL)
  424. BEGIN
  425. SET @Error=ERROR_MESSAGE();
  426. SET @Message= @Message+' - store sp_DMS_Baseline_DistributorSellInTarget failed: ' +@Error + '<br/>';
  427. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSellInTarget' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  428. END
  429. END catch
  430.  
  431. BEGIN try
  432. SET @Parameter = '@_CompanyID = 3,@_FromDate=' + CONVERT(nvarchar(20),@_FromDate) + ',@_LastDayOfMonth=' + CONVERT(nvarchar(20),@_LastDayOfMonth) + ',@_DistributorID='+Isnull(@_DistributorID,'')
  433. SET @StartTime = getdate();
  434. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSales)
  435. EXEC dbo.sp_DMS_Baseline_DistributorSales @_CompanyID, @_FromDate, @_LastDayOfMonth, @_DistributorID
  436. SET @EndTime = GETDATE();
  437. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSales)
  438. SET @RowCountNumber = @EndRowCount - @StartRowCount
  439. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSales' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  440. END try
  441. BEGIN catch
  442. IF(ERROR_MESSAGE() IS NOT NULL)
  443. BEGIN
  444. SET @Error=ERROR_MESSAGE();
  445. SET @Message= @Message+' - store sp_DMS_Baseline_DistributorSales failed: ' +@Error + '<br/>';
  446. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSales' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  447. END
  448. END catch
  449.  
  450. BEGIN try
  451. SET @Parameter = '@_CompanyID = 3,@_Month=' + CONVERT(nvarchar(20),@_Month) + ',@_Year=' + CONVERT(nvarchar(20),@_Year)
  452. SET @StartTime = getdate();
  453. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSales)
  454. EXEC dbo.sp_DMS_Baseline_DistributorSystem @_CompanyID, @_Month, @_Year
  455. SET @EndTime = GETDATE();
  456. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSales)
  457. SET @RowCountNumber = @EndRowCount - @StartRowCount
  458. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystem' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  459. END try
  460. BEGIN catch
  461. IF(ERROR_MESSAGE() IS NOT NULL)
  462. BEGIN
  463. SET @Error=ERROR_MESSAGE();
  464. SET @Message= @Message+' - store sp_DMS_Baseline_DistributorSystem failed: ' +@Error + '<br/>';
  465. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystem' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  466. END
  467. END catch
  468.  
  469. BEGIN try
  470. SET @Parameter = '@_CompanyID = 3,@_Month=' + CONVERT(nvarchar(20),@_Month) + ',@_Year=' + CONVERT(nvarchar(20),@_Year)
  471. SET @StartTime = getdate();
  472. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSystem)
  473. EXEC dbo.sp_DMS_Baseline_DistributorSystem @_CompanyID, @_Month, @_Year
  474. SET @EndTime = GETDATE();
  475. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSystem)
  476. SET @RowCountNumber = @EndRowCount - @StartRowCount
  477. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystem' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  478. END try
  479. BEGIN catch
  480. IF(ERROR_MESSAGE() IS NOT NULL)
  481. BEGIN
  482. SET @Error=ERROR_MESSAGE();
  483. SET @Message= @Message+' - store sp_DMS_Baseline_DistributorSystem failed: ' +@Error + '<br/>';
  484. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystem' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  485. END
  486. END catch
  487.  
  488.  
  489. BEGIN try
  490. SET @Parameter = '@_CompanyID = 3,@_Month=' + CONVERT(nvarchar(20),@_Month) + ',@_Year=' + CONVERT(nvarchar(20),@_Year)
  491. SET @StartTime = getdate();
  492. SET @StartRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSystemByCustomer)
  493. EXEC dbo.sp_DMS_Baseline_DistributorSystemByCustomer @_CompanyID, @_Month, @_Year
  494. SET @EndTime = GETDATE();
  495. SET @EndRowCount = (SELECT COUNT(1) FROM DMSBLDistributorSystemByCustomer)
  496. SET @RowCountNumber = @EndRowCount - @StartRowCount
  497. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystemByCustomer' , @Parameter, 1,'' ,@StartTime , @EndTime,@RowCountNumber
  498. END try
  499. BEGIN catch
  500. IF(ERROR_MESSAGE() IS NOT NULL)
  501. BEGIN
  502. SET @Error=ERROR_MESSAGE();
  503. SET @Message= @Message+' - store sp_DMS_Baseline_DistributorSystemByCustomer failed: ' +@Error + '<br/>';
  504. EXEC dbo.spInsertHistory 'sp_DMS_Baseline_DistributorSystemByCustomer' , @Parameter, 0,@Message, @StartTime , @EndTime,@RowCountNumber;
  505. END
  506. END catch
  507.  
  508. EXEC BELDMS..pp_DMS_L2_SendInforAfterBaseline @CompanyID,@_CheckDate,@_FromDate,@_ToDate
  509.  
  510.  
  511. END
  512. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement