以下是查询代码:
using (var connection = Connection) {
string commandText;
OdbcCommand command;
if (loadedNodes.Count > 0)
{
commandText = "SELECT * FROM TABLENAME where (PAR_1 = ? OR PAR_2 = ?) and PAR_1 NOT IN ({0}) and PAR_2 NOT IN ({0}) LIMIT ?";
var paramNames = loadedNodes.Select((s, index) => $"'@param{index}'").ToArray();
var inClause = string.Join(", ", paramNames);
command = new OdbcCommand(string.Format(commandText, inClause), connection);
for (int i = 0; i < paramNames.Length; i++)
{
command.Parameters.AddWithValue(paramNames[i], loadedNodes[i]);
}
}
else
{
commandText = "SELECT * FROM TABLENAME where (PAR_1 = ? OR PAR_2 = ?) LIMIT ?";
command = new OdbcCommand(commandText, connection);
}
command.Parameters.Add("@PAR_1", OdbcType.VarChar).Value = inn;
command.Parameters.Add("@PAR_2", OdbcType.VarChar).Value = inn;
command.Parameters.Add("", OdbcType.Int).Value = limit;//!!!
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
relatedRegistries.Add(Mapper.Resolve().Map<NdsRegistry>(reader));
}
}
}
在第一种情况下,我使用""
传递了限制参数-工作正常。
在第二种情况下,commandText
对象中的command
看起来像:
SELECT * FROM TABLENAME where (PAR_1 = ? OR PAR_2 = ?) and PAR_1 NOT IN ('@param0') and PAR_2 NOT IN ('@param0') LIMIT ?
我在告诉错误:
ERROR [HY000] [Hortonworks][Phoenix] (2020) An error occured while executing statement:
8org.apache.calcite.avatica.proto.Responses$ErrorResponse‘
ћjava.lang.RuntimeException: org.apache.phoenix.schema.TypeMismatchException: ERROR 203 (22005): Type mismatch. INTEGER and VARCHAR
at org.apache.calcite.avatica.jdbc.JdbcMeta.propagate(JdbcMeta.java:683)
at org.apache.calcite.avatica.jdbc.JdbcMeta.execute(JdbcMeta.java:878)
at org.apache.calcite.avatica.remote.LocalService.apply(LocalService.java:268)
at org.apache.calcite.avatica.remote.Service$ExecuteRequest.accept(Service.java:1026)
at org.apache.calcite.avatica.remote.Service$ExecuteRequest.accept(Service.java:1002)
at org.apache.calcite.avatica.remote.AbstractHandler.apply(AbstractHandler.java:94)
at org.apache.calcite.avatica.remote.ProtobufHandler.apply(ProtobufHandler.java:46)
at org.apache.calcite.avatica.server.AvaticaProtobufHandler.handle(AvaticaProtobufHandler.java:127)
at org.apache.phoenix.shaded.org.eclipse.jetty.server.handler.HandlerList.handle...
有人可以建议,没有=
符号的情况下将参数传递到odbc命令的正确方法是什么?