-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusp_Populate_RemoteStoreOperationsBonus_Weekly.sql
80 lines (67 loc) · 2.14 KB
/
usp_Populate_RemoteStoreOperationsBonus_Weekly.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
/* ================================================================
Project Name: VOC
Module Name: [dbo].[usp_Populate_RemoteStoreOperationsBonus_Weekly]
Purpose:
To update future weeks in [RemoteStoreOperations].[Bonus] for users
who haven't updated their status in current week.
Parameter Info
None
Return Info
None
Output:
[RemoteStoreOperations].[Bonus] is updated with latest data.
Test Scripts:
EXEC [dbo].[usp_Populate_RemoteStoreOperationsBonus_Weekly]
CHANGE HISTORY
===================================================================
ID DESCRIPTION AUTHOR DATE
***** *********** ******** **********
4623 Created v-arjain 04/08/2020
===================================================================*/
ALTER PROCEDURE [dbo].[usp_Populate_RemoteStoreOperationsBonus_Weekly]
AS
BEGIN
BEGIN TRY
WITH CTE_NextWeek
AS
(SELECT DISTINCT EMAIL FROM RemoteStoreOperations.Bonus
WHERE WeekFilter = CONVERT(NVARCHAR, GETDATE()+1, 101) +'-'+CONVERT(NVARCHAR, GETDATE()+7, 101)
)
INSERT INTO RemoteStoreOperations.Bonus
SELECT
[Name]
,[Email]
,[Store]
,[OptInStatus]
,[Assignment]
,[BonusEligibility]
,NULL AS [BonusAwarded]
,DATEADD(minute,DATEDIFF(minute,[UTCCreatedDate],[CreatedDate]),GETUTCDATE()) AS [CreatedDate]
,[CreatedBy]
,CONVERT(NVARCHAR, GETDATE()+1, 101) +'-'+CONVERT(NVARCHAR, GETDATE()+7, 101) AS [WeekFilter]
,GETUTCDATE() AS [UTCCreatedDate]
FROM
(
SELECT *, ROW_NUMBER () OVER(PARTITION BY [Email] ORDER BY ID DESC) AS RowNumber FROM
[RemoteStoreOperations].[bonus] (NOLOCK)
) AS B
WHERE RowNumber=1
AND Email NOT IN (SELECT Email FROM CTE_NextWeek)
END TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE()
,@ErrorSeverity = ERROR_SEVERITY()
,@ErrorState = ERROR_STATE();
-- Use RAISERROR inside the CATCH block to return
RAISERROR (
@ErrorMessage
,-- Message text
@ErrorSeverity
,-- Severity
@ErrorState -- State
);
END CATCH
END