Compare two sets of XML data using XQuery in SQL Server

后端 未结 3 1622
野的像风
野的像风 2021-01-02 08:13

Suppose I store employee data in a xml column in my log table. Sometimes data is also updated in the xml column from a stored procedure.

He

3条回答
  •  心在旅途
    2021-01-02 08:51

    I am too late here !!! However I found that if employees XML as shown above has multiple records then the JOIN query with CTE returns incorrect results.

    I have below XML input

    DECLARE @XML1 XML
    DECLARE @XML2 XML
    
    SET @XML1 = 
    ' 
    
        
             keith 
             1005 
            12/02/1981
            ACC001
            10,500
        
        
             keith 
             1004 
            12/02/1981
            ACC001
            10,500
        
    
    '
    
        SET @XML2 = 
        ' 
        
            
                 keith 
                 1005 
                12/02/1981
                ACC001
                10,500
            
            
                 keith 
                 1004 
                12/02/1981
                ACC001
                10,501
            
            
                 keith1 
                 10040 
                12/02/1981
                ACC001
                10,501
            
        
        '
    

    I will use below query to find the difference

    select  T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
    T.N.value('.', 'nvarchar(100)') as Value
    from @XML2.nodes('/NewDataSet/Employees/Employee/*') as T(N)
    
    EXCEPT
    
    select  T.N.value('local-name(.)', 'nvarchar(100)') as NodeName,
    T.N.value('.', 'nvarchar(100)') as Value
    from @XML1.nodes('/NewDataSet/Employees/Employee/*') as T(N)
    

    Hope this helps !!!

提交回复
热议问题