Conditional insert xquery failing with error "unexpected insert, expecting else"

381 views Asked by At

I am trying to execute the xquery on Sedna database to conditionally update the container, as below

if(fn:exists(doc("blog")/entries/entry[@active="1" and @id="1"]/comments)) then
UPDATE insert <comment active="1"><name>sunil.s</name><email>[email protected]</email><desc>desbbh</desc></comment> into doc("blog")/entries/entry[@active="1" and @id="1"]/comments
else
UPDATE insert <comments><comment active="1"><name>sunil.s</name><email>[email protected]</email><desc>sdd</desc></comment></comments> into doc("blog")/entries/entry[@active="1" and @id="1"]

But this query always failing with below error

SEDNA Message: ERROR XPST0003 It is a static error if an expression is not a valid instance of the grammar defined in A.1 EBNF. Details: at (2:8), syntax error, unexpected insert, expecting else

The error indicate that it is expecting else instead of insert in the second line.

Can someone please help me understand problem with the query and possible fix?

1

There are 1 answers

0
Michael Dyck On

Your query assumes the existence of an expression with syntax like

UPDATE insert expr into expr

There is no such expression in XQuery (or the XQuery Update Facility). Instead, it appears to be a non-standard syntax supported by Sedna.

However, the documentation (http://www.sedna.org/progguide/ProgGuidesu6.html#x12-430002.3) refers to it as a "statement", not an "expression", suggesting that it must be the 'top-level' (outermost) construct in your query.

To accomplish this, you could rewrite your query as:

UPDATE
insert
    if ... then <comment>...</comment> else <comments>...</comments>
into
    if ... then doc("blog")/.../comments else doc("blog")/...

Unfortunately, this repeats the 'if' condition; it's not clear whether Sedna provides a syntax for factoring that out, e.g.

UPDATE
let $c := ...
insert
    if $c then <comment>...</comment> else <comments>...</comments>
into
    if $c then doc("blog")/.../comments else doc("blog")/...