How to optimise the 'XQuery' SQL

两盒软妹~` 提交于 2019-12-04 12:23:42

If you only want one value out of every row there is no need to use cross apply.

select XMLCol.value('(/*[local-name()=sql:variable("@Root")]
                      /*[local-name(.)=sql:variable("@Entity")]
                      /*[local-name(.)=sql:variable("@ParentNode")]
                      /*[local-name(.)=sql:variable("@Separator")]
                      /*[local-name(.)=sql:variable("@ChildNode")])[1]', 'varchar(max)')
from XMLTable

Another way to get the same is to use FLWOR. In my limited tests this will run a bit faster.

select XMLCol.value('(for $n1 in /*,
                          $n2 in $n1/*,
                          $n3 in $n2/*,
                          $n4 in $n3/*,
                          $n5 in $n4/*
                      where $n1[local-name(.) = sql:variable("@Root")] and
                            $n2[local-name(.) = sql:variable("@Entity")] and
                            $n3[local-name(.) = sql:variable("@ParentNode")] and
                            $n4[local-name(.) = sql:variable("@Separator")] and
                            $n5[local-name(.) = sql:variable("@ChildNode")]
                      return $n5
                     )[1]', 'varchar(max)')
from XMLTable
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!