Is there a way to create a "global" named variable?

We are running a lot of operations where the first statement in each is to get the set of nodes to operate on. For many, its the same set of nodes over and over again. Its not predictable time to time so its not something I can just assign an attribute for. Normally, I just have a starting node, run the tree and collect all the subnodes. Then I run about 106 different cypher statements on that collection.

But each query has to "re collect" the nodes. That seems like a waste of time since its the same nodes the previous query collected. I could add a temporary attribute to make it simpler to find but that doesn't seem terribly efficient either.

Is there a way to create a set of nodes in a variable and have that variable persist query to query ?

Hello @bill.dickenson :slight_smile:

You should have a look at subqueries. You could collect nodes before and import them for each query in the call between UNION.

Regards,
Cobra

That worked but in the end, it also was too static for what we had in mind. Solution was good though.

I like to set up a mapped object that I can use throughout the query.

WITH {} as qparam
MATCH (nodes:N {id:$id})
WITH qparam {.*, nodes: COLLECT(nodes) }

now I just hand qparams to each part of the query and user the mapped object nodes.

Others have made good suggestions for aliasing and subqueries, provided that you can do all you need to in a single query.

If you need to process across multiple queries and transactions, you could always add a temporary label to all the nodes you need to work with, and for the remainder of the queries use that temporary label in your matches so you're always working with the same nodes. You'll just need to remember to remove the label when you're done, and ensure no other queries can come along and modify the labels while you're processing.