XQuery Update using IF Condition...

Wednesday, 2 January 2013 · 2 messages
JB
John Best2 Jan 2013 11:35 #
Dear Team, I am getting error - If expression : no updating expression allowed - while trying to update the "Data" DB. I need to update the DB, if it is not already updated...
for $c in doc('2012')/Docs/Doc/No/@n
    return
          if
(string-length(doc('Data')/Docs/Doc[No/@n=$c]/Title/Name/text()) eq 0) then
            insert node $c into doc('Data')/Docs
          else
            <l>{''}</l>
-- Have a nice day JBest
CG
Christian Grün2 Jan 2013 18:37 #
Hi John, all branches of your if expression must be updating, or return an empty sequence. In the latest snapshot [1], I’ve replaced the old error message with a more helpful one (“all expressions must be updating or return an empty sequence.”). To come back to your query: The following query will be accepted:
  for $c in doc('2012')/Docs/Doc/No/@n
  return
    if(doc('Data')/Docs/Doc[No/@n=$c]/Title/Name/text())
    then insert node $c into doc('Data')/Docs
    else ()
The following query does the same:
  for $c in doc('2012')/Docs/Doc/No/@n
  where doc('Data')/Docs/Doc[No/@n=$c]/Title/Name/text()
  return insert node $c into doc('Data')/Docs
If you want to return some textual output for all nodes that have not been updated, you can use db:output() [2]:
  for $c in doc('2012')/Docs/Doc/No/@n
  return
    if(doc('Data')/Docs/Doc[No/@n=$c]/Title/Name/text())
    then insert node $c into doc('Data')/Docs
    else db:output(<ld>{''}</l>)

⚡Generated with XQuery