Match statement inside Create

Hi guys.

Can anyone share with me a CREATE statement that has a match block inside to pull information and push into the properties set via the CREATE.

G

You can do it in a couple of ways:

First capture all the variables before the merge

MATCH (A:Node {property:'value'} )
WITH A.propertyToUse AS aProperty
MERGE (B:Another) SET B.target = aProperty

If you have complex statements, and are using variables, you can use apoc.cypher.run()

CALL apoc.cypher.run('MATCH (node {uuid: theVariable} ) RETURN someNode', { theVariable: 'some value' })
YIELD value
MERGE ...

(If memory doesn’t fail me - I understand later versions will allow for MATCH inside MERGE)

1 Like

hi there.

thanks

guessing for below…

MATCH (A:Node {property:'value'} )
WITH A.propertyToUse AS aProperty
MERGE (B:Another) SET B.target = aProperty

I would probably want to ask/do a match with B, assume thats what we’re doing via the (A:Node {property:'value'} ) i.e.

MATCH (A:Node {property:'value'} )
WITH A.propertyToUse AS aProperty
MATCH (B:Another {property:'value'} )
MERGE (B:Another) SET B.target = aProperty

?

MATCH (A:Node {property:'value1'} ) 
WITH A.propertyToUse AS aProperty 
MERGE (B:Another {property:'value2'} )
ON CREATE
 ... not sure if you would do anything?
ON MATCH 
  SET B.target = aProperty

Hi Josh

Thanks, give me some good options.

G

MATCH (A:Node {property:'value1'} ) 
WITH A.propertyToUse AS aProperty 
MERGE (B:Another {property:'value2'} )
ON CREATE
 ... not sure if you would do anything?
ON MATCH 
  SET B.target = aProperty
1 Like