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
| ALTER PROCEDURE [dbo].[sp_GA_ASSET_VALUES_01]
AS
BEGIN
SET NOCOUNT ON
DECLARE @StartDate datetime
, @BDD_TF sysname
, @BDD_DW sysname
, @sql nvarchar(max)
---
, @ExecutionId int
, @RuleTypeCode nvarchar(max)
, @Actif_TF_CountLine decimal(18,7)
, @Actif_TF_Fin_CountLine decimal(18,7)
, @DIFF01_ASSET_VALUES_01 varchar(32) -- Peut-être d'un autre type
, @DIFF02_ASSET_VALUES_01 varchar(32) -- Peut-être d'un autre type
SELECT @StartDate = GETDATE()
, @BDD_TF = dbo.fn_GetTransfertDatabase()
, @BDD_DW = dbo.fn_GetDWHDatabase()
SELECT @DIFF01_ASSET_VALUES_01 = ParameterValue
FROM dbo.ReperesLoadRuleParameters
WHERE ParameterCode = 'DIFF01_ASSET_VALUES_01'
SELECT @DIFF02_ASSET_VALUES_01 = ParameterValue
FROM dbo.ReperesLoadRuleParameters
WHERE ParameterCode = 'DIFF02_ASSET_VALUES_01'
-- Récupération de l'identifiant d'exécution
SET @sql = 'SELECT @ExecutionId = MAX(RLE.ExecutionId)'
+ 'FROM [' + @BDD_DW + '].dbo.ReperesLoadExecutions RLE'
+ 'JOIN ['+@BDD_DW+'].dbo.ReperesLoadProcesses RLP ON RLE.LoadProcessId = RLP.LoadProcessId'
+ 'WHERE RLP.ProcessCode = ''CHG_DTM_GA'''
EXEC sp_executeSQL
@sql
, '@ExecutionId int OUTPUT'
, @ExecutionId = @ExecutionId OUTPUT
-- Récupération du niveau de criticité
SET @sql = 'SELECT @RuleTypeCode = RuleTypeCode'
+ 'FROM [' + @BDD_DW + '].dbo.ReperesLoadRules'
+ 'WHERE RuleCode = ''ASSET_VALUES_01'''
EXEC sp_executeSQL
@sql
, '@RuleTypeCode nvarchar(max) OUTPUT'
, @RuleTypeCode = @RuleTypeCode OUTPUT
-- Récupération de @Actif_TF_CountLine
SET @sql = 'SELECT @Actif_TF_CountLine = SUM(AverageAssetValueEUR)'
+ 'FROM [' + @BDD_TF + '].dbo.MonthlyAssetValues'
EXEC sp_executeSQL
@sql
, '@Actif_TF_CountLine decimal(18,7) OUTPUT'
, @Actif_TF_CountLine = @Actif_TF_CountLine OUTPUT
-- Récupération de @Actif_TF_Fin_CountLine
SET @sql = 'SELECT @Actif_TF_Fin_CountLine = SUM(EndPeriodAssetValueEUR)'
+ 'FROM [' + @BDD_TF + '].dbo.MonthlyAssetValues'
EXEC sp_executeSQL
@sql
, '@Actif_TF_Fin_CountLine decimal(18,7) OUTPUT'
, @Actif_TF_Fin_CountLine = @Actif_TF_Fin_CountLine OUTPUT
-- Vérification de l'écart...
SELECT @Result = dbo.fn_ER
(
@Actif_TF_Fin_CountLine
, @Actif_TF_CountLine
, @DIFF01_ASSET_VALUES_01
, @DIFF02_ASSET_VALUES_01
)
SET @ResultStatus = LEFT(@Result, CHARINDEX('_', @Result) - 1)
SET @ResultValue = CAST(RIGHT(@Result, LEN(@Result) - CHARINDEX('_', @Result)) AS decimal(18,2))
-- Si ERROR et niveau de criticité BLOQUANT alors...
IF
(
@ResultStatus = 'ERROR'
AND @RuleTypeCode = 'BLOQUANT'
)
BEGIN
-- Insertion dans la table ReperesLoadExecutionResults...
INSERT dbo.ReperesLoadExecutionResults
(
ExecutionId
, RuleId
, ResultStatusCode
, ResultMessage
, ResultDetail
, StartDate
, EndDate
)
SELECT @ExecutionId
, RuleId
, @ResultStatus
, REPLACE(REPLACE(REPLACE(AlertMessage,'#{N1}',@Actif_TF_CountLine),'#{N0}',@Actif_TF_Fin_CountLine),'#{diff_pct*100}',CAST(@ResultValue*100. AS nvarchar(max))) AlertMessage
, ActionMessage
, @StartDate
, GETDATE()
FROM dbo.ReperesLoadRules
WHERE RuleCode = 'ASSET_VALUES_01'
-- Sortie en erreur
RAISERROR(N'Too many difference between Intruments number in TF and DW!', 16, 1)
END
-- Si WARNING et niveau de criticité BLOQUANT, ou WARNING/ERROR et niveau de criticité NON-BLOQUANT) alors...
IF
(
(
@ResultStatus = 'WARNING'
AND @RuleTypeCode IN('BLOQUANT', 'NON-BLOQUANT')
)
OR
(
@ResultStatus = 'ERROR'
AND @RuleTypeCode = 'NON-BLOQUANT'
)
)
BEGIN
-- Insertion dans la table ReperesLoadExecutionResults...
INSERT dbo.ReperesLoadExecutionResults
(
ExecutionId
, RuleId
, ResultStatusCode
, ResultMessage
, ResultDetail
, StartDate
, EndDate
)
SELECT @ExecutionId
, RuleId
, @ResultStatus
, REPLACE(REPLACE(REPLACE(AlertMessage,'#{N1}',@Actif_TF_CountLine),'#{N0}',@Actif_TF_Fin_CountLine),'#{diff_pct*100}',CAST(@ResultValue*100. AS nvarchar(max))) AlertMessage
, ActionMessage
, @StartDate
, GETDATE()
FROM dbo.ReperesLoadRules
WHERE RuleCode = 'ASSET_VALUES_01'
END
-- Si OK ou bien niveau de criticité "PASSANT" alors...
ELSE IF (@ResultStatus = 'OK' OR @RuleTypeCode = 'PASSANT')
BEGIN
-- Insertion dans la table ReperesLoadExecutionResults...
INSERT dbo.ReperesLoadExecutionResults
(
ExecutionId
, RuleId
, ResultStatusCode
, ResultMessage
, ResultDetail
, StartDate
, EndDate
)
SELECT @ExecutionId
, RuleId
, 'OK'
, '' AlertMessage
, '' ActionMessage
, @StartDate
, GETDATE()
FROM dbo.ReperesLoadRules
WHERE RuleCode = 'ASSET_VALUES_01'
END
END |
Partager