sql server - If condition on Selecting XML node in SQL -
i trying read xml , storing in sql server.
declare @xml xml set @xml = '<report> <personal> <search> <subject> <name>searchname</name> </subject> </search> </personal> <personal> <search> <subject> <name>searchname</name> </subject> </search> <result> <history> <name>historyname</name> </history> </result> </personal> </report> '
what trying here - selecting name condition here if <personal>
contains <result>
select name
under history/name
if <personal>
doesn't contain <result>
select name
under subject/name
currently selecting names personal/subject below:
select a.search.value('(subject/name)[1]','varchar(max)') @xml.nodes('/report/personal/search') a(search)
expecting result:
searchname historyname
how add condition in between?
is there way can add exists condition here
select @xml.exist('//report//personal//search//subject//name')
this:
select coalesce( a.search.value('(result/history/name)[1]','varchar(max)'), a.search.value('(search/subject/name)[1]','varchar(max)') ) name @xml.nodes('/report/personal') a(search)
will return:
name ------------ searchname historyname
Comments
Post a Comment