尝试在变量sql server中以xml格式存储结果

时间:2014-09-05 14:49:41

标签: sql-server xml

我只是想比较两个xml并尝试将差异存储到另一个名为@DiffXML的变量中,但会收到名为Incorrect syntax near the keyword 'SET'.的错误

只是指导修复的内容。感谢

DECLARE @XML1 XML
DECLARE @XML2 XML

DECLARE @DiffXML nvarchar(max)
SET @DiffXML=''

SET @XML1 = 
'<NewDataSet> 
<Employee>
<EmpID>1005</EmpID>
<Name> keith </Name>
<DOB>12/02/1981</DOB>
<DeptID>ACC001</DeptID>
<Salary>10,500</Salary>
</Employee>
</NewDataSet>'

SET @XML2 = 
'<NewDataSet> 
<Employee>
<EmpID>1006</EmpID>
<Name> keith </Name>
<DOB>05/02/1981</DOB>
<DeptID>ACC002</DeptID>
<Salary>10,900</Salary>
</Employee>
</NewDataSet>'

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

SET @DiffXML=(select * from 
(
    select coalesce(XML1.NodeName, XML2.NodeName) as FieldName, 
           XML1.Value as OldValue, 
           XML2.Value as NewValue
    from XML1
      full outer join XML2
        on XML1.NodeName = XML2.NodeName
    where coalesce(XML1.Value, '') <> coalesce(XML2.Value, '')    
) x FOR xml AUTO,elements XSINIL)

print @DiffXML

1 个答案:

答案 0 :(得分:1)

更改
SET @DiffXML=(select * from

SELECT @DiffXML=(select * from

您将获得打印结果