3

我有一个iscript,它运行以前创建的 SQL 语句集合中的一个,绑定几个参数,并生成 XML 结果。

每个请求中使用的 SQL 在参数数量和返回的列数(和名称)方面有所不同。

除了一个突出的问题之外,一切都非常简单:我如何收集列名并将该信息包含在返回的数据中?

目前我们正在使用CreateSQL命令和 SQL 对象。据我所知,我们只能遍历结果值,而不是列名和值的字典。

在 iscript 的上下文中,如何使用无法提前知道的(基本上)动态 SQL 在 PeopleCode 中返回列名和结果?

4

1 回答 1

1

I have a way to get the column name by PLSQL, just a little complex.

First, create a table to store a long string in the Application Designer:

-- create table
create table ps_sql_text_tbl 
(
  comments clob
)
tablespace hrapp
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 40k
    next 104k
    minextents 1
    maxextents unlimited
  );

Second, use DBMS_SQL in a new funciton:

create or replace function get_column_name return clob is
  l_curid      integer;
  l_cnt        number;
  l_desctab    dbms_sql.desc_tab3;
  l_sql        dbms_sql.varchar2s;
  l_upperbound number;
  l_stmt       clob;
  l_result     clob;
begin
  /*get a sql text into a clob var*/
  select comments into l_stmt from ps_sql_text_tbl where rownum = 1;

  /*200 chars for every substring*/
  l_upperbound := ceil(dbms_lob.getlength(l_stmt) / 200);
  for i in 1 .. l_upperbound loop
    l_sql(i) := dbms_lob.substr(l_stmt, 200, ((i - 1) * 200) + 1);
  end loop;

  l_curid := dbms_sql.open_cursor();

  /*parse the sql text*/
  dbms_sql.parse(l_curid, l_sql, 1, l_upperbound, false, dbms_sql.native);
  /*describe column names*/
  dbms_sql.describe_columns3(l_curid, l_cnt, l_desctab);
  /*concatenate all column names*/
  for i in 1 .. l_desctab.count loop
    /*max length limited to 30 chars for every column name*/
    l_result := l_result || rtrim(rpad(l_desctab(i).col_name,30)) || ';';
  end loop;

  dbms_sql.close_cursor(l_curid);

  return l_result;
exception
  when no_data_found then
    return '';
end get_column_name ;

Last, use peoplecode to get the column names:

Local string &sqlText="select * from dual";

SQLExec("truncate table ps_sql_text_tbl");
SQLExec("insert into ps_sql_text_tbl values(%TextIn(:1)) ", &sqlText);
SQLExec("commit");

Local string &columnNames;
SQLExec("select get_column_name() from dual", &columnNames);

Local array of string &arrayColumnNames= Split(&columnNames, ";");
于 2019-10-18T08:23:26.143 回答