DECLARE @ExistingSettings xml = '
';
DECLARE @NewSettings xml = '
';
WITH CTE_Actual_Settings AS (
SELECT
Settings.Setting.value('@name', 'nvarchar(100)') AS name,
Settings.Setting.value('@value', 'nvarchar(100)') AS value
FROM @ExistingSettings.nodes('/Settings/Setting') AS Settings(Setting)
), CTE_New_Settings AS (
SELECT
Settings.Setting.value('@name', 'nvarchar(100)') AS name,
Settings.Setting.value('@value', 'nvarchar(100)') AS value
FROM @NewSettings.nodes('/Settings/Setting') AS Settings(Setting)
), CTE_Merged_Settings AS (
SELECT
CASE WHEN A.name IS NULL THEN N.name ELSE A.name END AS name,
CASE WHEN N.name IS NOT NULL THEN N.value ELSE A.value END AS value
FROM CTE_Actual_Settings AS A
FULL OUTER JOIN CTE_New_Settings AS N
ON A.name = N.name
)
SELECT
name AS '@name',
value AS '@value'
FROM CTE_Merged_Settings
FOR XML PATH('Setting'), ROOT('Settings');
|