ATAN的Oracle查询 - 指定的强制转换无效

时间:2018-04-12 02:04:15

标签: c# sql .net oracle

我的代码如下:

con.Open();
OracleCommand cmd = CreateCommand();
cmd.Connection = con;               
cmd.CommandText = $"SELECT SUM(ATAN((CASE WHEN (t0.CustomerID = 'Test') THEN 1 ELSE 1 END))) value FROM Customers  t0 WHERE(t0.CustomerID = 'Test')";
cmd.CommandType = CommandType.Text;
var reader = cmd.ExecuteReader();                              
while (reader.Read())
{
    decimal value = reader.GetDecimal(0);
}

我得到的错误 - “指定的演员表无效”

有人可以帮帮我吗? 提前致谢。 俊

2 个答案:

答案 0 :(得分:0)

您的查询是否返回一行?只是想知道这是否是由返回null的可能性引起的。

SQL> with customers as
  2   ( select 'Test' CustomerID from dual )
  3  SELECT SUM(ATAN((CASE WHEN (t0.CustomerID = 'Test') THEN 1 ELSE 1 END))) value FROM Customers  t0 WHERE(t0.CustomerID = 'Test');

     VALUE
----------
.785398163

1 row selected.

SQL>
SQL> with customers as
  2   ( select 'Test1' CustomerID from dual )
  3  SELECT SUM(ATAN((CASE WHEN (t0.CustomerID = 'Test') THEN 1 ELSE 1 END))) value FROM Customers  t0 WHERE(t0.CustomerID = 'Test');

     VALUE
----------
     (null)

上面的后一种情况或许是原因?

答案 1 :(得分:0)

我解决了这个问题如下:

con.Open();
OracleCommand cmd = CreateCommand();
cmd.Connection = con;               
cmd.CommandText = $"SELECT SUM(ATAN((CASE WHEN (t0.CustomerID = 'Test') THEN 1 ELSE 1 END))) value FROM Customers  t0 WHERE(t0.CustomerID = 'Test')";
cmd.CommandType = CommandType.Text;
var reader = cmd.ExecuteReader();                              
while (reader.Read())
{
  decimal value = reader.GetOracleDecimal(0);
}

谢谢大家。