Skip to content
Advertisement

XQuery [value()] issue for Dynamic SQL variable in Select Stateemt

I’m try to make the code below dynamic as there can be different names for the ‘name’ attribute in the page ‘node’.

When I run the code below it returns the error ‘XQuery [value()]: ‘value()’ requires a singleton (or empty sequence), found operand of type ‘xdt:untypedAtomic *”

I feel my main issue is in the line ‘C.value(‘(/level1/level2/template/page/@name)[sql:variable(“@Counter”)]’,’NVARCHAR(MAX)’) AS [Page Name]’ where I am trying to make ‘@counter’ variable dynamic.

Does anyone have any solutions please?

XML FOR ID 9371

RESULTS FROM SQL Query

** Present Result

** Desired Result

Advertisement

Answer

I think

might suffice to produce the wanted result without the need for a WHILE loop.

I think the original problem with the long path inside of value() (e.g. C.value('(/level1/level2/template/page/@name)[sql:variable("@Counter")]','NVARCHAR(MAX)')) is due to the static type checking of XQuery in SQL server and to avoid it you basically need to add another predicate that ensures the type checker knows a single value is returned e.g. C.value('(/level1/level2/template/page/@name)[sql:variable("@Counter")][1]','NVARCHAR(MAX)')

For me, the code

produces the table

so at least the Page_Name seems to be easily populated by using e.g. C.value('@name','NVARCHAR(MAX)') AS [Page Name]

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement