Oracle ResultSetMetaData getPrecision/getScale

人走茶凉 提交于 2019-12-04 12:49:47
elMartino

Oracle can not return a type based on a view or the count(*) as it was not explicitly declared. Your view could return any precision or scale depending on the underlaying tables of the view.

To overcome this you would need to cast the type in your query or view like this:

select CAST (count(*) AS NUMBER(30))

An alternative is to query user_tab_columns or all_tab_columns.

A scale of 0 is acceptable: a NUMBER(5) is the same as a NUMBER(5,0)

The precision however must be an integer between 1 and 38 when it is defined. When it is undefined, as in NUMBER, the driver has to return something since it cannot return null. In that case the driver chooses to return 0.

It seems to me that there is no comprehensive documentation on ResultSetMetaData. Oracle® Database JDBC Developer's Guide and Reference 10g Release 2 (10.2) and 11g Release 2 (11.2) gives example about column name and type here, they does not deal with other aspects.

Someone had a similar problem with PostgreSQL years ago and he made a patch. Maybe Oracle uses the same codebase here.

You may try to use the ojdbc14_g.jar instead of ojdbc14.jar as its classes were compiled with "javac -g" and contain some tracing information.

You may also try newer drivers.

You can use the rs.getBigDecimal(columnIndex) and from the big decimal you can get the precision / scale values of the specific columns.

Not a direct reply to your question, but a workaround you mentioned:

If all you have to do is check or compare db schemas, then instead of ResultSetMetaData and querying all the tables use Oracle schema information as described in Reverse Engineering a Data Model. I used it in my utility to export such information to text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!