开发者

"SELECT 1 FROM ... " in oracle returns a decimal type

开发者 https://www.devze.com 2022-12-13 01:36 出处:网络
I\'m getting a datatable from sample query below in ORACLE \"SELECT 1 AS X FROM ... \" when i select the X by using dr.Field(\"X开发者_如何学运维\"), it gives me error.

I'm getting a datatable from sample query below in ORACLE "SELECT 1 AS X FROM ... "

when i select the X by using dr.Field("X开发者_如何学运维"), it gives me error. I've found out that the it returns as decimal datatype.

Using the same statement in MSSQL has no problem, i've got the int datatype.

Any workaround or solution to this?


An integer in Oracle is a Number with precision 0. Since you are not specifying the size (can be larger than 16) it is cast to decimal.

Try casting it to number(16,0) and you should be ok.

For the people suggesting ODP.NET: it gives the same problem.


Your front-end code is mis-autodetecting the type of the field. You should be able to override it to be an integer instead. Without more details on the language / etc being used, it's hard to be more specific.


If you know the type of that column should be an int and assuming it's not null:

int x = Convert.ToInt32 (dr.Field ["X"]);

This will not have any effect with other DB providers that return an int for that column.

If you want to change your Oracle query to return an int instead:

SELECT CAST(1 AS INTEGER) FROM ...


I don't have my Oracle books here at home, but an explicit convert or cast to integer would probably be the way to go. (Speaking of the Oracle query, not your C# that retrieves the result, although you could convert there as well.) "1" is ambiguous, as far as Oracle is concerned.


What I needed to do in order to get an int instead of a decimal was:

SELECT CAST(1 as NUMBER(9,0)) FROM Dual

0

精彩评论

暂无评论...
验证码 换一张
取 消