如何在Neo4j中将参数传递给APOC查询

时间:2018-11-27 13:38:27

标签: neo4j neo4j-apoc

我正在尝试使用APOC库从SQL DB查询记录(在一定时期之间)。当我将起始日期和结束日期作为参数传递时,出现错误。

代码:

String queryString ="CALL apoc.load.jdbc('mssql',\"select * from table as tb where tb.from_Date >= {fromDate} AND tb.to_Date <= {endDate} \") YIELD row";

String fromDate = "2018-11-12 00:00:00";
String endDate = "2018-11-13 00:00:00";
Map<String, Object> parameters = new HashMap<>();
parameters.put("fromDate", fromDate);
parameters.put("endDate", endDate);

StatementResult result = tx.run(queryString, parameters);

我遇到以下错误:

如果我使用语法{}

Neo.ClientError.Procedure.ProcedureCallFailed: Failed to invoke procedure 
`apoc.load.jdbc`: Caused by: 
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'.

如果我使用语法$

Neo.ClientError.Procedure.ProcedureCallFailed: Failed to invoke procedure 
`apoc.load.jdbc`: Caused by: 
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid pseudocolumn "$fromDate".

1 个答案:

答案 0 :(得分:0)

在您的版本中,参数占位符只是查询字符串的一部分,它们的值未传递。

因此,请像这样使用apoc.load.jdbcParams过程:

String query = "CALL apoc.load.jdbc('mssql', {sql}, [{fromDate}, {endDate}]) YIELD row return row";
String sql = "select * from table as tb where tb.from_Date >= ? AND tb.to_Date <= ?";
String fromDate = "2018-11-12 00:00:00";
String endDate = "2018-11-13 00:00:00";

Map<String, Object> parameters = new HashMap<>();
parameters.put("sql", sql);
parameters.put("fromDate", fromDate);
parameters.put("endDate", endDate);

StatementResult result = tx.run(query, parameters);