USE master


BEGIN TRANSACTION 
SET XACT_ABORT ON;
SET NOCOUNT ON;

DECLARE @lists TABLE(
    listname VARCHAR(8000) NOT NULL 
    ,recipients VARCHAR(MAX) NOT NULL 
);

INSERT INTO @lists ([listname],
                    [recipients])
VALUES('HCI_Consultants_Facturation','anne-claude.terrettaz@galenica.com')
,('HCI_Facturation','anne-claude.terrettaz@galenica.com; arnaud.jolliet@galenicare.com')
,('HCI_PHInsurance_Monitoring','Gal_SE_DBA@galenica.com')
,('HCI_Responsable_Centrale_Amavita','david.saugy@galenica.com; gilles.balanche@galenica.com')
,('HCI_Consultants_Pharmacies_Facturation','anne-claude.terrettaz@galenica.com')
;


--SELECT * 
--FROM [Arizona].[dbo].[DBA_Mailing_list]
--WHERE [DML_Code] IN (SELECT [listname] FROM @lists)

--SELECT * 
--FROM [HCITools].[dbo].[DBA_Mailing_list]
--WHERE [DML_Code] IN (SELECT [listname] FROM @lists)

IF EXISTS(SELECT 1 FROM sys.databases WHERE name ='hcitools')
BEGIN 

    ----part of V sync, do not alter manually
    --UPDATE m SET [m].[DML_Recipients] = s.[recipients]
    --FROM [Arizona].[dbo].[DBA_Mailing_list] m
    --    JOIN @lists s ON s.[listname] = m.dml_code
    --;
    --PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - update arizona list. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';

    UPDATE m SET [m].[DML_Recipients] = s.[recipients]
    FROM [HCITools].[dbo].[DBA_Mailing_list] m
        JOIN @lists s ON s.[listname] = m.dml_code
    ;
    PRINT CONVERT(VARCHAR(20), CURRENT_TIMESTAMP, 114)+' - updated hciTools lis. '+REPLACE(REPLACE(CONVERT(VARCHAR(100), CONVERT(MONEY, @@rowcount), 1),',',''''),'.00','')+' row(s) affected.';

END 

--SELECT * 
--FROM [Arizona].[dbo].[DBA_Mailing_list]
--WHERE [DML_Code] IN (SELECT [listname] FROM @lists)

--SELECT * 
--FROM [HCITools].[dbo].[DBA_Mailing_list]
--WHERE [DML_Code] IN (SELECT [listname] FROM @lists)

--ROLLBACK TRANSACTION 
COMMIT TRANSACTION 