# Use of WITH clause to declare variables;

**URL:** <https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540>\
**Category:** Cypher\
**Created:** [March 20, 2023, 1:28pm UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540 "2023-03-20T13:28:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcantoni](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pcantoni](https://community.neo4j.com/u/pcantoni)\
**Post date:** [March 20, 2023, 1:28pm UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540/1 "2023-03-20T13:28:41Z")

</div>

I've been using the WITH clause to decalre variables for later use such as...  
WITH 'inherits from\>' AS caption  
MATCH (bv:Vertex {name:'«Root»'})  
MATCH (iv:Vertex {name:'Datastore'})  
MATCH (abv:ArcBase {name:'INHERITANCE'})  
MERGE (iv)-[ibi:INHERITANCE {name:iv.name+'-'+abv.name+'-\>'+bv.name}]-\>(bv)  
ON CREATE  
SET ibi.caption=caption,  
ibi.originName=iv.name, ibi.originUUID=iv.uuid,  
ibi.destinationName=bv.name, ibi.destinationUUID=bv.uuid   
ON MATCH  
SET ibi.caption=caption,  
ibi.originName=iv.name, ibi.originUUID=iv.uuid,  
ibi.destinationName=bv.name, ibi.destinationUUID=bv.uuid   
;  
Is this an appropriate use of the clause? If so, why does the above version work, but  
MATCH (bv:Vertex {name:'«Root»'})  
MATCH (iv:Vertex {name:'Datastore'})  
MATCH (abv:ArcBase {name:'INHERITANCE'})  
WITH 'inherits from\>' AS caption  
MERGE (iv)-[ibi:INHERITANCE {name:iv.name+'-'+abv.name+'-\>'+bv.name}]-\>(bv)  
ON CREATE  
SET ibi.caption=caption,  
ibi.originName=iv.name, ibi.originUUID=iv.uuid,  
ibi.destinationName=bv.name, ibi.destinationUUID=bv.uuid   
ON MATCH  
SET ibi.caption=caption,  
ibi.originName=iv.name, ibi.originUUID=iv.uuid,  
ibi.destinationName=bv.name, ibi.destinationUUID=bv.uuid   
;  
i.e. moving the WITH clause to _below_ the matches cause the subsequent MERGE to fail?

TIA,  
Paolo

---

<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:** [March 20, 2023, 2:05pm UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540/2 "2023-03-20T14:05:29Z")

</div>

The with 'with' clauses passes variables to the next phase of the query. In your second example, your 'with' clause only defines 'caption', so all the variables you defined before hand go out of scope following the 'with' clause. You need to do something like the following:

```auto
with iv, abv, bv, 'inherits from>' AS caption

```

Note, 'with' can do more than define variables. It can also be used to group and aggregate/collect data

---

<div class="post-metadata">

**Author:** ![pcantoni](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pcantoni](https://community.neo4j.com/u/pcantoni)\
**Post date:** [March 20, 2023, 10:25pm UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540/3 "2023-03-20T22:25:18Z")

</div>

Thanks, @glilienfield,  
We observed the behaviour you described and _surmised_ that was the reason. But we didn't **know**... Now, we do!

Paolo

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [March 21, 2023, 12:01am UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540/4 "2023-03-21T00:01:17Z")

</div>

@pcantoni

further described in the doc at [WITH - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/clauses/with/) and

```auto
It is important to note that WITH affects variables in scope. Any variables
 not included in the WITH clause are not carried over to the rest of the 
query. The wildcard * can be used to include all variables that are 
currently in scope.

```

note the link to doc is for Neo4j 5.x doc. Now it should be the same for prior versions as well but as version was never described in the original post.

---

<div class="post-metadata">

**Author:** ![pcantoni](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pcantoni](https://community.neo4j.com/u/pcantoni)\
**Post date:** [March 21, 2023, 12:43am UTC](https://community.neo4j.com/t/use-of-with-clause-to-declare-variables/61540/5 "2023-03-21T00:43:15Z")

</div>

Thanks, @dana_canzano; I'm now using Neo4j 5.x so, all good!

Paolo
