简体   繁体   English

在DB2中按列索引号而不是列名选择

[英]Select by column index number instead of column name in DB2

Is it possible to get the value of a column based on a column index? 是否可以基于列索引获取列的值? Something like the sql below: 类似于下面的sql:

SELECT 
    (SELECT colname 
     FROM syscat.columns 
     WHERE tabname = 'myTable' AND colno=100) AS TEST 
FROM myTable

The above sql returns only the column name of the specified index, not the actual value. 上面的sql仅返回指定索引的列名,而不返回实际值。

SQL is typed. 输入SQL。 The system needs to know which data type will be returned because there is a difference between a char(1), an integer and a BLOB type. 系统需要知道将返回哪种数据类型,因为char(1),整数和BLOB类型之间存在差异。 Others have pointed out to use a stored procedure with dynamic SQL inside. 其他人指出要在内部使用带有动态SQL的存储过程 However, even stored procedures have typed parameters. 但是,即使存储过程也具有类型化的参数。 There are options to be relative generic for the parameters, but then those parameter structures have types attached. 有一些参数可以相对通用,但是这些参数结构具有附加的类型。

What should work with functions and procedures is to 应与功能和程序配合使用的是

  1. retrieve the column name and data type, then 检索列名称和数据类型,然后
  2. prepare and execute a dynamic statement to process that desired column inside the function/procedure. 准备并执行动态语句以处理函数/过程中的所需列。

It's possible by using PureXML: 使用PureXML是可能的:

SELECT 
    XMLQUERY('$X[2]/text()'
        PASSING XMLCONCAT(
            XMLELEMENT(NAME "col1", first),
            XMLELEMENT(NAME "col2", middle),
            XMLELEMENT(NAME "col3", last) 
            --- . . .
            --- and so on
        ) AS "X"
    )
FROM employee

You change the xquery expression index number $X[ 2 ]/text() to access the wanted column. 您可以更改xquery表达式索引号$X[ 2 ]/text()来访问所需的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM