Параметры OracleCommand Query Null в where- ORA-01008: не все переменные связаны
У меня есть следующий метод, и оба параметра являются нулевыми
bool hasValue = HasValue(null,null);
internal bool HasValue(int? param1, int? param2)
{
int count = 0;
using (var conn = new OracleConnection(connectionString))
{
using (var command = conn.CreateCommand())
{
command.CommandText = "select count(id) from Table1 "
+ "where ((Column1 = :PARAM1 and :PARAM1 Is Not Null) Or (Column1 Is Null and :PARAM1 Is Null)) "
+ "AND ((Column2 = :PARAM2 and :PARAM2 Is Not Null) Or (Column2 Is Null and :PARAM2 Is Null))";
command.Parameters.Add("PARAM1", OracleDbType.Int16, 0, param1, ParameterDirection.Input);
command.Parameters.Add("PARAM2", OracleDbType.Int16, 0,param2, ParameterDirection.Input);
command.Connection.Open();
count = Convert.ToInt16(command.ExecuteScalar());
command.Connection.Close();
}
}
return count > 0;
}
Сбой метода со следующей ошибкой "ORA-01008: не все переменные связаны"
Если я использую только один параметр, то это нормально, но когда я добавляю второй, он выходит из строя с "ORA-01008: не все переменные связаны"
заранее спасибо
1 ответ
Решение
command.CommandText = "select count(id) from Table1 "
+ "where decode(Column1, :PARAM1, 1) = 1 AND decode(Column2, :PARAM2, 1) = 1";