【问题标题】:How to get column names for SQL object in PeopleCode?如何在 PeopleCode 中获取 SQL 对象的列名?
【发布时间】:2019-10-07 14:51:20
【问题描述】:

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

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

除了一个悬而未决的问题:我如何收集列名并将该信息包含在返回的数据中?

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

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

【问题讨论】:

    标签: oracle peoplesoft peoplecode


    【解决方案1】:

    我有办法通过PLSQL获取列名,就是有点复杂。

    首先,在应用程序设计器中创建一个表来存储一个长字符串:

    -- 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
      );
    

    其次,在新功能中使用 DBMS_SQL:

    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 ;
    

    最后,使用 peoplecode 获取列名:

    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, ";");
    

    【讨论】:

    • 我将与我的 PeopleSoft 人员核实一下,看看这是否可行!谢谢你。我会回来告诉你的。
    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多