2 Replies Latest reply: Dec 12, 2011 12:41 AM by tsuji RSS

    [Xquery] How to make an increment of variable in a For loop

    tien86
      Hello,

      How can i build a simple increment for variable $a by Xquery such as ?

      let $a := 0
      for $i in (1 to 10)
      let $a := $a + 1
      return $a

      why $a value in this loop always is '1'.

      Thank you for reading, its will really helpful for my job if i can solve this problem.
        • 1. Re: [Xquery] How to make an increment of variable in a For loop
          AlexAnd
          something?
          SQL> select xmlquery (
            2  'let $a := 0
            3  for $i in 1 to 10
            4  let $a := $a + $i
            5  return $i'
            6  returning content) v
            7  FROM dual
            8  /
          
          V
          --------------------------------------------------------------------------------
          1 2 3 4 5 6 7 8 9 10
          
          SQL> 
          • 2. Re: [Xquery] How to make an increment of variable in a For loop
            tsuji
            I think the op might mean otherwise.

            In fact, the xquery variables (say $a here) are "immutable"... you can think of it that the assignment statement is evaluated left to right rather than like procedural language right to left. But the figurative description is only suggestive. Eventually, it means that $a is first set up anew when the statement is encountered, then it is assigned a value $a+1 which is then 1 each time!

            Instead, use the keyword "at" for this specific construction.
            for $i at $a in (1 to 10)
            return $a