SQL XML DIFF on attributes -
i have xml field in table , want set trigger store changes in table. want know example attribute "url" changed "http://example.com/" "http://newurl.com" , when.
this have now:
declare @xml1 xml declare @xml2 xml set @xml1 = '<newdataset> <employee> <empid>1005</empid> <name> keith </name> <dob>12/02/1981</dob> <deptid>acc001</deptid> </employee> </newdataset>' set @xml2 = '<newdataset> <employee> <empid>1005</empid> <name> keith </name> <dob>12/02/1981</dob> <deptid>acc002</deptid> </employee> </newdataset>' ;with xml1 ( select t.n.value('local-name(.)', 'nvarchar(100)') nodename, t.n.value('.', 'nvarchar(100)') value @xml1.nodes('/newdataset/employee/*') t(n) ), xml2 ( select t.n.value('local-name(.)', 'nvarchar(100)') nodename, t.n.value('.', 'nvarchar(100)') value @xml2.nodes('/newdataset/employee/*') t(n) ) select coalesce(xml1.nodename, xml2.nodename) nodename, xml1.value oldval, xml2.value newval, getutcdate() changed xml1 full outer join xml2 on xml1.nodename = xml2.nodename coalesce(xml1.value, '') <> coalesce(xml2.value, '')
but works node values, , want work attributes too.
example of xml file want work:
<crawlsetup> <go param="" url="http://www.example.com/index.php"> <match param="" match="href="(/job[^"]+)"" url="http://www.example.com$1" save1="" save2="" save3=""> <match param="" match="href="(/job[^"]+)"" url="http://www.example.com$1" save1="" save2="" save3="" /> <next match="" url="$1" /> </match> </go> </crawlsetup>
there might several attributes, , want detect changes in attributes.
can me on this? =)
if want select 1 attribute (e.g. url) of element use xpath expression :
from @xml2.nodes('/crawlsetup/go/@url') t(n)
to select attributes of element use 1 :
from @xml2.nodes('/crawlsetup/go/@*') t(n)
Comments
Post a Comment