尝试这样的事情 -
DECLARE
@oldXML NVARCHAR(MAX)
, @newXML NVARCHAR(MAX)
SELECT
@oldXML = '<root><row USER_KEY="1" USER_NAME="test" USER_ID="12345" USER_STATUS=" " USER_GENDER="F" /></root>'
, @newXML = '<root><row USER_KEY="1" USER_NAME="test" USER_ID="00000" USER_STATUS=" " USER_GENDER="F" /></root>'
;WITH cte AS
(
SELECT id, t.rn, token = SUBSTRING(name, 1, CHARINDEX('" ', t.name) - 1)
FROM (
SELECT
name =
SUBSTRING(
t.string
, number + 2
, ABS(CHARINDEX('="', t.string, number + 1) - number - 1))
, rn = ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY (SELECT 1))
, id
FROM (
SELECT id = 1, string = @newXML
UNION ALL
SELECT id = 2, @oldXML
) t
CROSS JOIN [master].dbo.spt_values n
WHERE [type] = 'p'
AND number <= LEN(t.string) - 1
AND SUBSTRING(t.string, number, 2) = '="'
) t
)
SELECT t2.token
FROM (
SELECT *
FROM cte
WHERE id = 1
) t1
LEFT JOIN (
SELECT *
FROM cte
WHERE id = 2
) t2 ON t1.rn = t2.rn AND t1.token != t2.token
WHERE t2.token IS NOT NULL