# Query seems to not execute properly. Is it an issue?

**URL:** <https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465>\
**Category:** Neo4j Graph Platform\
**Tags:** migrated\
**Created:** [January 24, 2023, 2:20pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465 "2023-01-24T14:20:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mv59\_grid](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@mv59\_grid](https://community.neo4j.com/u/mv59_grid)\
**Post date:** [January 24, 2023, 2:20pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465/1 "2023-01-24T14:20:53Z")

</div>

Hello there,

I am executing queries which seems to behave weird.  
The queries i am executing is this one :

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'62c6df857aeb04026be826069df0faa67236a7d7d10a6b848fc66acf4b7ab7ab'})
CALL apoc.periodic.iterate(
  " WITH $matchchilds AS matchchilds UNWIND matchchilds as matchchild RETURN matchchild",
  " MATCH(nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})  
    MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
{batchSize:1000, parallel:false,params:{matchchilds: $matchchilds}}
) 
YIELD batch, operations  
RETURN batch ,operations , nodea0

```

The query runs fine with this output :

```auto
[
    Record {
      keys: ['batch', 'operations', 'nodea0'],
      length: 3,
      _fields: [
        {
          total: Integer { low: 6, high: 0 },
          committed: Integer { low: 6, high: 0 },
          failed: Integer { low: 0, high: 0 },
          errors: {}
        },
        {
          total: Integer { low: 5732, high: 0 },
          committed: Integer { low: 5732, high: 0 },
          failed: Integer { low: 0, high: 0 },
          errors: {}
        },
        Node {
          identity: Integer { low: 173480, high: 0 },
          labels: ['DOPLER', 'Maquette', 'ImportXML', 'HypoComplexe'],
          properties: {
            Attr_dtCreation: "'13/10/2022 14:14'",
            Attr_description: "''",
            nameUser: "''",
            Attr_createur: "'gsradm'",
            name: "'Etat 9'",
            Attr_nom: "'Etat '",
            id: '62c6df857aeb04026be826069df0faa67236a7d7d10a6b848fc66acf4b7ab7ab',
            Attr_type: "'20'",
            Attr_dtModif: "'13/10/2022 14:14'",
            Attr_xmlns: "'http://www. *****.com/gsr'",
            Attr_xmlns_xsi: "'http://www.w3.org/2001/XMLSchema-instance'"
          },
          elementId: '4:15b97476-e1db-4513-8d0c-610cb672c8ca:173480'
        }
      ],
      _fieldLookup: { batch: 0, operations: 1, nodea0: 2 }
    }
]

```

So nodea0 seems to have been found becaue it is returned in the result. The query has just to MERGE the relation between nodea1 and nodeb list.  
nodeb variable is matched through a parameter list with 5732 values.  
But in my graph, i am left with an empty node like this linked to each nodeb (it seems that variable nodea1 contains void at execution time) :

![mv59_grid_1-1674569369977.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/6/26412ab81ea3a9594cfbb810b05584b9c36978f3.png "mv59\_grid\_1-1674569369977.png")

Expected output is more like :

![mv59_grid_0-1674570258462.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/f/4/f4bf8fb2df4c48d46a595b60c37c7b5febea1d4a.png "mv59\_grid\_0-1674570258462.png")

I tried many alternatives without success (i didn't get error though) :

- Subquery the apoc procedure with a with clause on nodea0

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'62c6df857aeb04026be826069df0faa67236a7d7d10a6b848fc66acf4b7ab7ab'})  
CALL { 
  WITH nodea0 
  CALL apoc.periodic.iterate(
     " WITH $matchchilds AS matchchilds UNWIND matchchilds as matchchild RETURN matchchild",
  " MATCH (nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})
    MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
  {batchSize:1000, parallel:false,params:{matchchilds: $matchchilds}}
   ) 
   YIELD batch, operations  
   RETURN batch ,operations
} 
RETURN batch,operations, nodea0

```

- add nodea0 as apoc procedure parameter :

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'62c6df857aeb04026be826069df0faa67236a7d7d10a6b848fc66acf4b7ab7ab'})  
CALL { 
   WITH nodea0 
   CALL apoc.periodic.iterate(
      " WITH $matchchilds AS matchchilds UNWIND matchchilds as matchchild RETURN matchchild",
      " MATCH (nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})  
        MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
      {batchSize:1000, parallel:false,params:{matchchilds: $matchchilds, nodea0:nodea0}}
    ) 
    YIELD batch, operations 
    RETURN batch ,operations 
}
RETURN batch,operations, nodea0

```

What am i doing wrong?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 24, 2023, 4:15pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465/2 "2023-01-24T16:15:31Z")

</div>

Yes, your issues is that you did not pass node nodea0 to the apoc procedure. The node is not available to the second apoc query otherwise. You also can not use a parameter as a node variable in a pattern, thus you need to use a 'with' statement to set the parameter to a variable. The following should work.

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'62c6df857aeb04026be826069df0faa67236a7d7d10a6b848fc66acf4b7ab7ab'})
CALL apoc.periodic.iterate(
  " UNWIND $matchchilds as matchchild 
    RETURN matchchild ",
  " WITH $nodea0 AS nodea0 
    MATCH(nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})  
    MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
{batchSize:1000, parallel:false,params:{matchchilds: $matchchilds, nodea0: nodea0}}
) 
YIELD batch, operations  
RETURN batch ,operations , nodea0

```

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [January 24, 2023, 5:02pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465/3 "2023-01-24T17:02:23Z")

</div>

\*\*bleep\*\*, I left out 'matchchilds' on the 'with' on line 5. Some of the apoc procedures provide a 'with' statement in the code to map each parameter to a variable with the same name as the parameters. apoc.periodic.iterate does not do this.

```auto
WITH $nodea0 AS nodea0, $matchchilds as matchchilds

```

---

<div class="post-metadata">

**Author:** ![mv59\_grid](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@mv59\_grid](https://community.neo4j.com/u/mv59_grid)\
**Post date:** [January 24, 2023, 4:43pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465/4 "2023-01-24T16:43:50Z")

</div>

I tried to modify the query -\>

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'633635e8a432b6c6f87f36b93df207491942bb7a0714a791725c70d6d070a5bc'})  
   CALL apoc.periodic.iterate(
     "WITH $matchchilds AS matchchilds UNWIND matchchilds as matchchild RETURN matchchild",
     "WITH $nodea0 AS nodea0
      MATCH(nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})  
      MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
    {batchSize:1000, parallel:false,params:{matchchilds: $matchchilds, nodea0:nodea0}}
    ) 
YIELD batch, operations
RETURN batch ,operations 

```

It seems to complain about parameters (query works fine (including parameters) without proposed modifications :

```auto
[
    Record {
      keys: ['batch', 'operations'],
      length: 2,
      _fields: [
        {
          total: Integer { low: 6, high: 0 },
          committed: Integer { low: 0, high: 0 },
          failed: Integer { low: 6, high: 0 },
          errors: {
            'org.neo4j.graphdb.QueryExecutionException: Variable `matchchild` not defined (line 1, column 140 (offset: 139))\n"UNWIND $_batch AS _batch WITH _batch.matchchild AS matchchild WITH $nodea0 AS nodea0 MATCH (nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id}) MERGE (nodea0)-[rel0:CONTAINS]->(nodeb)"\n ^': Integer { low: 6, high: 0 }
          }
        },
        {
          total: Integer { low: 5732, high: 0 },
          committed: Integer { low: 0, high: 0 },
          failed: Integer { low: 5732, high: 0 },
          errors: {
            'Variable `matchchild` not defined (line 1, column 140 (offset: 139))\n"UNWIND $_batch AS _batch WITH _batch.matchchild AS matchchild WITH $nodea0 AS nodea0 MATCH (nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id}) MERGE (nodea0)-[rel0:CONTAINS]->(nodeb)"\n ^': Integer { low: 6, high: 0 }
          }
        }
      ],
      _fieldLookup: { batch: 0, operations: 1 }
    }
  ]

```

Is there any error in my modified query?

---

<div class="post-metadata">

**Author:** ![mv59\_grid](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@mv59\_grid](https://community.neo4j.com/u/mv59_grid)\
**Post date:** [January 24, 2023, 4:53pm UTC](https://community.neo4j.com/t/query-seems-to-not-execute-properly-is-it-an-issue/58465/5 "2023-01-24T16:53:29Z")

</div>

Found the solution.  
WITH must conflicts with first return value.

You must add matchchild in the second WITH clause.  
Working solution :

```auto
MATCH(nodea0:HypoComplexe:DOPLER:Maquette:ImportXML{id:'633635e8a432b6c6f87f36b93df207491942bb7a0714a791725c70d6d070a5bc'})  
CALL apoc.periodic.iterate(
   "WITH $matchchilds AS matchchilds UNWIND matchchilds as matchchild RETURN matchchild",
   "WITH $nodea0 AS nodea0, matchchild AS matchchild 
    MATCH(nodeb:Hypothese:DOPLER:Maquette:ImportXML{id:matchchild.params1_id})  
    MERGE (nodea0)-[rel0:CONTAINS]->(nodeb) ",
   {batchSize:1000, parallel:false,params:{matchchilds: $matchchilds, nodea0:nodea0}}
)
YIELD batch, operations
RETURN batch ,operations 

```

It was a tough one but now it works.

Thank you.
