SQL Server异常:使用JDBC时“列名xxx无效”

时间:2021-06-24 01:53:40

I'm getting a strange error from the SQL Server JDBC driver. It is telling me that a column name is invalid even though the column is present, correctly named and the same query works fine when executed in SqlServer Management Studio.

我从SQL Server JDBC驱动程序中收到一个奇怪的错误。它告诉我列名无效,即使该列存在,正确命名并且在SqlServer Management Studio中执行时相同的查询正常工作。

The error is:

错误是:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The column name MarginCall is not valid.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(SQLServerResultSet.java:626)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getBigDecimal(SQLServerResultSet.java:2570)
    at org.apache.commons.dbcp.DelegatingResultSet.getBigDecimal(DelegatingResultSet.java:305)
...

3 个解决方案

#1


12  

Problem solved. It was a simple mistake by me.

问题解决了。这是我的一个简单错误。

My query was using an 'AS' clause in the select statement. I was trying to retrieve the column value from the ResultSet using the actual column name, instead of the column alias defined in the AS clause.

我的查询是在select语句中使用'AS'子句。我试图使用实际的列名从ResultSet中检索列值,而不是在AS子句中定义的列别名。

Schoolboy error. Apologies for the time wasting.

小学生错误。道歉浪费时间。

Many thanks to Steve B. for his suggestion to use ResultSet.getColumnNames(). Although the actual method call I used was ResultSet.getMetaData().getColumnName(columnIndex);

非常感谢Steve B.建议使用ResultSet.getColumnNames()。虽然我使用的实际方法调用是ResultSet.getMetaData()。getColumnName(columnIndex);

#2


1  

Check the case of your table name. if collation is set to case sensitive on ms sql server the table names are affected too.

检查表名的大小写。如果在ms sql server上将collat​​ion设置为区分大小写,则表名也会受到影响。

#3


0  

Try to enclose the column name inside square brackets: [MarginCall] Just a guess.

尝试将列名括在方括号内:[MarginCall]只是一个猜测。

#1


12  

Problem solved. It was a simple mistake by me.

问题解决了。这是我的一个简单错误。

My query was using an 'AS' clause in the select statement. I was trying to retrieve the column value from the ResultSet using the actual column name, instead of the column alias defined in the AS clause.

我的查询是在select语句中使用'AS'子句。我试图使用实际的列名从ResultSet中检索列值,而不是在AS子句中定义的列别名。

Schoolboy error. Apologies for the time wasting.

小学生错误。道歉浪费时间。

Many thanks to Steve B. for his suggestion to use ResultSet.getColumnNames(). Although the actual method call I used was ResultSet.getMetaData().getColumnName(columnIndex);

非常感谢Steve B.建议使用ResultSet.getColumnNames()。虽然我使用的实际方法调用是ResultSet.getMetaData()。getColumnName(columnIndex);

#2


1  

Check the case of your table name. if collation is set to case sensitive on ms sql server the table names are affected too.

检查表名的大小写。如果在ms sql server上将collat​​ion设置为区分大小写,则表名也会受到影响。

#3


0  

Try to enclose the column name inside square brackets: [MarginCall] Just a guess.

尝试将列名括在方括号内:[MarginCall]只是一个猜测。