CREATE procedure [C6MartPeriodico].[RP_DM_QD_VAR_RISK_WEEK2]  
AS 
BEGIN  
TRUNCATE TABLE [C6MARTPeriodico].[QD_VAR_RISK_WEEK2]
INSERT INTO [C6MARTPeriodico].[QD_VAR_RISK_WEEK2]
           ([banca],
			[rete],
			[codfis],
			[piva],
			[codfid],
			[codman],
			[chiave_acn],
			[isp_total_risk],
			[flag_risk])
			SELECT      
			[banca],
			[rete],
			[codfis],
			[piva],
			[codfid],
			[codman],
         CASE WHEN ISNULL(LTRIM(RTRIM(piva)), '') <> '' AND ISNULL(LTRIM(RTRIM(codman)), '') = '' THEN piva
              WHEN ISNULL(LTRIM(RTRIM(piva)), '') <> '' AND ISNULL(LTRIM(RTRIM(codman)), '') <> '' THEN 'FF@' + codman
              ELSE codfis 
              END AS chiave_acn,
			[isp_total_risk],
			[flag_risk]
  FROM [C6StagingPeriodico].[QD_VAR_RISK_WEEK2]
END