Utkarsh Goyal
Utkarsh Goyal

Reputation: 19

To chain together neo4j queries

I have three neo4j queries which I have to combine and run as one query, I have tried to combine but its giving an error.

The individual queries are:

First:

CREATE (r:Recommendation {companyId: 2, impactTimeFrame: "Short-Term", recommendationName: "Hello1", startTime:"Immediately" , recommendationType:"On-Off" , recommendationDetails:"Hello", probablity: "mid"})

Second:

MATCH (r: Recommendation {companyId:2, recommendationName:"Hello1" }) WITH r MATCH (p:Param { paramId:"Wellbeing" }) MERGE (r)-[re:HAS_IMPACT_ON]->(p)

Third:

MATCH (r: Recommendation { recommendationName:"Hello1", companyId: 2})WITH r (d:Demographic {companyId : 2,demographicName:"After Sales"}) MERGE (r)-[re:DEMOGRAPHIC_IMPACT_ON]->(d)

My code of combined three queries:

CREATE (r:Recommendation {companyId: 2, impactTimeFrame: "Short-Term", recommendationName: "Hello1", startTime:"Immediately" , recommendationType:"On-Off" , recommendationDetails:"Hello", probablity: "mid"})WITH r 
 MATCH (r: Recommendation {companyId:2, recommendationName:"Hello1" }) WITH r MATCH (p:Param { paramId:"Wellbeing" }) WITH r,p MERGE (r)-[re:HAS_IMPACT_ON]->(p) WITH r
MATCH (r: Recommendation { recommendationName:"Hello1", companyId: 2})WITH r MATCH(d:Demographic {companyId : 2,demographicName:"After Sales"}) WITH r,d MERGE (r)-[re:DEMOGRAPHIC_IMPACT_ON]->(d)

Individual queries are running fine but the combine one in giving an error, please help what wrong I am doing.

Thanks

Upvotes: 0

Views: 765

Answers (1)

Lju
Lju

Reputation: 590

You are re-using your variable r in your 2nd statement but not using it. When you've created/matched/merged a node, you don't need to keep finding it, that's why we use the WITH statement to keep a reference to it.

Perhaps re-write your query as following:

CREATE (r:Recommendation {companyId: 2, impactTimeFrame: "Short-Term", recommendationName: "Hello1", startTime:"Immediately" , recommendationType:"On-Off" , recommendationDetails:"Hello", probablity: "mid"})
WITH r 
MATCH (p:Param { paramId:"Wellbeing" }) 
MERGE (r)-[re:HAS_IMPACT_ON]->(p) 
WITH r
MATCH(d:Demographic {companyId : 2,demographicName:"After Sales"}) 
MERGE (r)-[re:DEMOGRAPHIC_IMPACT_ON]->(d)

Upvotes: 3

Related Questions