PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/PATTON/597RoyalCUCommercial/Stored Procedures/dbo.spExpirePointsExtractRev01.StoredProcedure.sql

https://bitbucket.org/augeomarketing/dover-database
SQL | 123 lines | 79 code | 20 blank | 24 comment | 0 complexity | bb27ec802c93d8ac45d13a453c81bc7e MD5 | raw file
  1. USE [597RoyalCUCommercial]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[spExpirePointsExtractRev01] Script Date: 09/25/2009 14:41:59 ******/
  4. SET ANSI_NULLS OFF
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. /********************************************************************************/
  9. /* This will Create the Expired Points Table for Monthly Processing */
  10. /* This process will take the current monthenddate and subtract 3 years */
  11. /* All points older than this date are subject to expiration */
  12. /* %%%%%% Note This version of the procedure creates the expiring points */
  13. /* table based on the current month end year minus 3 to expire points */
  14. /* this table is then used BY spProcessExpiredPoints to update the */
  15. /* Customer and History Tables */
  16. /* BY: B.QUINN */
  17. /* DATE: 3/2007 */
  18. /* REVISION: 0 */
  19. /* */
  20. /********************************************************************************/
  21. CREATE PROCEDURE [dbo].[spExpirePointsExtractRev01] @MonthEndDate NVARCHAR(10) AS
  22. --declare @MonthEndDate NVARCHAR(10)
  23. --set @MonthEndDate='11/30/2009'
  24. declare @DateOfExpire NVARCHAR(10), @ExpireYear char(4)
  25. set @ExpireYear=right( @MonthEndDate, 4)
  26. SET @DateOfExpire = '12/31/' + @ExpireYear
  27. Declare @PointsEarned int
  28. Declare @Pointsredeemed int
  29. Declare @PointsToExpire int
  30. DECLARE @SQLUpdate nvarchar(1000)
  31. DECLARE @SQLDynamic nvarchar(1000)
  32. declare @expirationdate nvarchar(25)
  33. declare @intday int
  34. declare @intmonth int
  35. declare @intyear int
  36. declare @ExpireDate DATETIME
  37. set @ExpireDate = cast(@DateOfExpire as datetime)
  38. SET @intYear = DATEPART(year, @ExpireDate)
  39. SET @intmonth = DATEPART(month, @ExpireDate)
  40. SET @intday = DATEPART(day, @ExpireDate)
  41. set @intYear = @intYear - 5
  42. set @expirationdate = (rtrim(@intYear) + '-' + rtrim(@intmonth) + '-' + rtrim(@intday) + ' 23:59:59.997')
  43. /*print 'year'
  44. print @intYear
  45. print 'month'
  46. print @intmonth
  47. print 'day'
  48. print @intday
  49. print '@expirationdate'
  50. print @expirationdate */
  51. TRUNCATE TABLE ExpiredPoints
  52. INSERT into ExpiredPoints
  53. SELECT tipnumber, sum(points * ratio) as addpoints,
  54. '0' AS REDPOINTS, '0' AS POINTSTOEXPIRE, '0' as PREVEXPIRED, @ExpireDate as dateofexpire
  55. from history
  56. where histdate < @expirationdate and (trancode not like('R%') and
  57. trancode <> 'IR'and trancode not like('XP'))
  58. group by tipnumber
  59. UPDATE ExpiredPoints
  60. SET PREVEXPIRED = (SELECT SUM(POINTS* RATIO)
  61. FROM HISTORY
  62. WHERE
  63. (trancode ='XP')
  64. AND TIPNUMBER = ExpiredPoints.TIPNUMBER)
  65. WHERE EXISTS (SELECT *
  66. FROM HISTORY
  67. WHERE
  68. trancode ='XP'
  69. AND TIPNUMBER = ExpiredPoints.TIPNUMBER)
  70. UPDATE ExpiredPoints
  71. SET REDPOINTS = (SELECT SUM(POINTS* RATIO)
  72. FROM HISTORY
  73. WHERE
  74. (trancode like('R%') or
  75. trancode = 'IR')
  76. AND TIPNUMBER = ExpiredPoints.TIPNUMBER)
  77. WHERE EXISTS (SELECT *
  78. FROM HISTORY
  79. WHERE
  80. (trancode like('R%') or
  81. trancode = 'IR')
  82. AND TIPNUMBER = ExpiredPoints.TIPNUMBER)
  83. UPDATE ExpiredPoints
  84. SET POINTSTOEXPIRE = (ADDPOINTS + REDPOINTS + PREVEXPIRED),
  85. dateofexpire = @ExpireDate
  86. UPDATE ExpiredPoints
  87. SET POINTSTOEXPIRE = '0'
  88. WHERE
  89. POINTSTOEXPIRE IS NULL
  90. UPDATE ExpiredPoints
  91. SET POINTSTOEXPIRE = '0'
  92. WHERE
  93. POINTSTOEXPIRE < '0'
  94. delete from ExpiredPoints
  95. WHERE
  96. POINTSTOEXPIRE = '0'
  97. update monthly_statement_file
  98. set PointsToExpire='0'
  99. update monthly_statement_file
  100. set PointsToExpire=b.pointstoexpire
  101. from monthly_statement_file a, expiredpoints b
  102. where a.tipnumber=b.tipnumber
  103. GO