问题
I'm getting this error when trying to run a query in my application and I'm not sure why:
Caused by: com.sybase.jdbc3.jdbc.SybSQLException: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.
at com.sybase.jdbc3.tds.Tds.a(Unknown Source)
at com.sybase.jdbc3.tds.Tds.nextResult(Unknown Source)
at com.sybase.jdbc3.jdbc.ResultGetter.nextResult(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.nextResult(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.nextResult(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.queryLoop(Unknown Source)
at com.sybase.jdbc3.jdbc.SybStatement.executeQuery(Unknown Source)
at com.sybase.jdbc3.jdbc.SybPreparedStatement.executeQuery(Unknown Source)
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeQuery(WSJdbcPreparedStatement.java:678)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
... 79 more
No where in my code am I trying to convert a VARCHAR into an INT.
Database View Columns --> Java type:
INT --> Integer
VARCHAR --> String
INT --> Long
VARCHAR --> a POJO
FLOAT --> Double
DATE --> Date
Is there anything else that may cause this error??
回答1:
Implicit conversion from datatype 'VARCHAR' to 'INT' will occur when you fail to enclose a VARCHAR value in quotes.
Implicit conversion from datatype 'INT' to 'VARCHAR' will occur when you enclose an INT value in quotes.
回答2:
In my case the default values provided for few INT columns in the table definition had VARCHAR values.
Example :
jobCount INT DEFAULT '0'
回答3:
@user1505078 is correct
In this C code, I use DB-Library DBSMALLINT type to search into a VARCHAR column, the return type is also an Integer so I had to wrap %d with single quotes (i.e. '% d'). That was enough to "convert" the value, since whatever it is going between quotes in that query will be CHAR anyway.
DBSMALLINT region_code;
dbbind(dbproc, 1, SMALLBIND, (DBINT)0, (BYTE *)®ion_code); /* Integer */
sybase_error = False;
dbfcmd(dbproc, "insert into <table_name> ");
dbfcmd(dbproc, "select * from <another_table> ");
/* With quotes, the query searches for Chars instead */
dbfcmd(dbproc, "where group_code = '%d' ", region_code); /* Quotes in '%d' */
dbsqlexec(dbproc);
while (dbresults(dbproc) != NO_MORE_RESULTS)
continue;
来源:https://stackoverflow.com/questions/9541117/sybase-error-implicit-conversion-from-datatype-varchar-to-int-not-allowed