Currently I use this query to show the differences:
SELECT
CASE
WHEN PaymentMethodCRM <> PaymentMethodBOU THEN 'Payement Method CRM / BOU'
WHEN PaymentMethodCRM <> PaymentMethodFicheERP THEN 'Payement Method CRM / ERP'
WHEN PaymentDeadlineCRM <> PaymentDeadlineBOU THEN 'Payment Deadline CRM / BOU'
WHEN PaymentDeadlineCRM <> PaymentDeadlineFicheERP THEN 'Payment Deadline CRM / ERP'
WHEN EditModeBOU <> EditModeContratERP THEN 'Edit Mode BOU / ERP'
WHEN LTRIM(RTRIM(CompanyNameCRM)) <> LTRIM(RTRIM(CompanyNameBOU)) THEN 'Company Name CRM / BOU'
WHEN LTRIM(RTRIM(CompanyNameBOU)) <> LTRIM(RTRIM(CAST(CompanyNameFicheERP AS NVARCHAR(50)))) THEN 'Company Name BOU / ERP'
ELSE ''
END AS Verification
FROM
Temp
But how to do if there are multiple differences?
My goal it’s to show it like this:
Payement Method CRM / BOU ; Payement Method CRM / ERP ; Payment Deadline CRM / ERP
I don’t want to have twenty lines of code to test each case
>Solution :
In SQL-Server 2016; You can do it using STUFF and FOR XML PATH.
SELECT STUFF((SELECT '; ' + DIFFERENCE
FROM (
SELECT
CASE
WHEN PaymentMethodCRM <> PaymentMethodBOU THEN 'Payement Method CRM / BOU'
WHEN PaymentMethodCRM <> PaymentMethodFicheERP THEN 'Payement Method CRM / ERP'
WHEN PaymentDeadlineCRM <> PaymentDeadlineBOU THEN 'Payment Deadline CRM / BOU'
WHEN PaymentDeadlineCRM <> PaymentDeadlineFicheERP THEN 'Payment Deadline CRM / ERP'
WHEN EditModeBOU <> EditModeContratERP THEN 'Edit Mode BOU / ERP'
WHEN LTRIM(RTRIM(CompanyNameCRM)) <> LTRIM(RTRIM(CompanyNameBOU)) THEN 'Company Name CRM / BOU'
WHEN LTRIM(RTRIM(CompanyNameBOU)) <> LTRIM(RTRIM(CAST(CompanyNameFicheERP AS NVARCHAR(50)))) THEN 'Company Name BOU / ERP'
ELSE ''
END AS DIFFERENCE
FROM
Temp
) AS DIFFERENCES_TABLE
WHERE DIFFERENCE <> ''
FOR XML PATH('')), 1, 2, '') AS DIFFERENCES;
In SQL-Server 2017; You can use STRING_AGG for the same.