我正在尝试使用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".
答案 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);