I have a XML column in database that may look like that ones:
<sql-connection-info name="myname" server="(local)\SQLEXPRESS" other-attribute="value" />
<oracle-connection-info name="othername" server="address" other-attribute="value" />
and so on. The names of nodes and attributes can be nearly anything. I need to iterate over the attribute/value pair on the first node. Every sample I have seen was for known node/attribute names.
When I tried to use
@xmlColumn.query("/@*")
I get this error
XQuery [query()]: Top-level attribute nodes are not supported.
Is this possible in TSQL? If yes how can I do this?
declare @xmlColumn xml = '<sql-connection-info name="myname" server="(local)\SQLEXPRESS" other-attribute="value" />'
select T.N.value('local-name(.)', 'varchar(max)') as Name,
T.N.value('.', 'varchar(max)') as Value
from @xmlColumn.nodes('//@*') as T(N)
Result:
Name Value
---------------- -------------------
name myname
server (local)\SQLEXPRESS
other-attribute value
You can use
@xmlColumn.query("/node()[1]")
to get the first node of each entry. node()
matches any element node. From your post I do not understand if you want to have eg the name
attribute of the first node of your entry. Then you would use:
@xmlColumn.query("/node()[1]/@name")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.