【问题标题】:Is it mandatory to use an ALIAS when we are doing some operation on the column?当我们对列进行一些操作时,是否必须使用别名?
【发布时间】:2009-11-20 11:16:17
【问题描述】:

各位, 当我们对列进行一些操作时,是否必须使用别名?

例如:从 table1 中选择 upper(col1)
当我试图通过 rs.getString("col1") 检索结果集时,它给出了这个异常 COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0611E 无效的列名。 SQLSTATE=S0022

当我将查询更改为:
select upper(col1) as col1 from table1 并使用 rs.getString("col1") 时,它工作正常。

那么,在列上应用诸如上部、修剪、下部等功能时是否必须使用别名?

我正在使用:DB2 8.2,Type 2 驱动程序

谢谢

【问题讨论】:

    标签: exception db2 alias


    【解决方案1】:

    表达式 UPPER(COL1) 与 COL1 相同。

    比较一下:

    $ db2 "describe select col1 from session.t1"
    
    SQLDA Information
    
     sqldaid : SQLDA     sqldabc: 1136  sqln: 20  sqld: 1
    
     Column Information
    
     sqltype               sqllen  sqlname.data                    sqlname.length
     --------------------  ------  ------------------------------  --------------
     453   CHARACTER           10  COL1                                         4
    

    到这里:

    $ db2 "describe select upper(col1) from session.t1"
    
    SQLDA Information
    
     sqldaid : SQLDA     sqldabc: 1136  sqln: 20  sqld: 1
    
     Column Information
    
     sqltype               sqllen  sqlname.data                    sqlname.length
     --------------------  ------  ------------------------------  --------------
     453   CHARACTER           10  1                                            1
    

    请注意,每个结果集中的列名 (sqlname.data) 是不一样的。

    因此,您必须使用列别名或使用 rs.getString("1")。

    【讨论】:

      【解决方案2】:

      改用rs.getString("upper(col1)")?通常,结果中的列名包括应用于该列的任何函数。否则,您将无法获得 SELECT MIN(col1),MAX(col1) ... 之类的结果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-06
        • 2014-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        • 2018-03-15
        • 2017-10-25
        相关资源
        最近更新 更多