You could just check the value of @@ROWCOUNT
and if it's 0, then perform the INSERT
of the "default" values:
INSERT INTO gstl_calculated_daily_fee (business_date,
fee_type,
fee_total,
range_id)
SELECT @tlf_business_date,
'SWITCH_FEE_LOCAL_CARD',
SUM(C.settlement_fees),
C.range_id
FROM (SELECT *
FROM (
-- set a row number or transaction number per transaction which resets every month
SELECT @previous_mada_switch_fee_volume_based_count + (ROW_NUMBER() OVER (PARTITION BY DATEPART(MONTH, x_datetime)ORDER BY x_datetime)) AS rowNumber,
tt.x_datetime
FROM gstl_trans_temp tt
WHERE (message_type_mapping = 0220)
AND card_type = 'GEIDP1'
AND response_code IN (00, 10, 11)
AND tran_amount_req >= 5000
AND merchant_type NOT IN (5542, 5541, 4829)) A
-- cross apply to calculate which range for each transaction
CROSS APPLY (SELECT rtt.settlement_fees,
rtt.range_id
FROM gstl_mada_local_switch_fee_volume_based rtt
WHERE A.rowNumber >= rtt.range_start
AND (A.rowNumber <= rtt.range_end
OR rtt.range_end IS NULL)) B ) C
-- group by date to get the per date fees
GROUP BY CAST(C.x_datetime AS date), --Seems this is in the GROUP BY and not the SELECT. Intentional?
C.range_id;
IF @@ROWCOUNT = 0
INSERT INTO gstl_calculated_daily_fee (business_date,
fee_type,
fee_total,
range_id)
VALUES (@tlf_business_date, 'SWITCH_FEE_LOCAL_CARD', NULL, NULL);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…