将控制台输出数据从pl / sql插入Temp Table

时间:2015-03-23 08:51:43

标签: oracle plsql

我有下面的pl / sql,它从不同的表中获取并打印多个列,并在控制台上打印。但是对于实时场景,我需要将所有数据放入临时表中。因此,用户可以从临时表中执行Select *并获取检索到的数据。有人可以确认我该怎么做?

    SET SERVEROUTPUT ON
    Declare
    Cursor crsr(companyschema varchar2) is Select Person_Id,Birth_Name from salesdemo.PER_PERSON;
    personId number;
    bioBirthName varchar2(128);
    personalBirthName varchar2(128);
    usersSysId varchar2(256);
    legalEntity number;
    territoryId number;
    country varchar2(256);
    BEGIN
      FOR c_schema IN
      (SELECT company_schema
      FROM sap.sf_companies c,
        sap.sf_feature_map m
      WHERE c.company_id = m.company_id
      AND m.feature_id   = 326  
      ) 
      LOOP
      BEGIN

      open crsr(c_schema.company_schema);
        if crsr%isopen then
        loop
        begin
          fetch crsr into personId, bioBirthName;
          exit when crsr%notfound;
          dbms_output.Put_line('Schema '|| c_schema.company_schema);
          dbms_output.Put_line('PersonId '|| personId);
          dbms_output.Put_line('Bio Birth Name '|| bioBirthName);
          execute immediate 'select Birth_Name  from '||c_schema.company_schema||'EMP_PERSONAL_INFO_T where person_id = :1' INTO personalBirthName using personId;
          dbms_output.Put_line('Personal Birth Name '|| personalBirthName);
          execute immediate 'select users_sys_id  from '||c_schema.company_schema||'EMP_EMPLOYMENT_INFO where person_id = :1' INTO usersSysId USING personId;
          dbms_output.Put_line('UsersSysId '|| usersSysId);
          execute immediate 'select company  from '||c_schema.company_schema||'EMP_JOB_INFO_T where users_sys_id = :1 ' INTO legalEntity USING usersSysId ;
          dbms_output.Put_line('Legal Entity '|| legalEntity);
          execute immediate 'select territory_id  from '||c_schema.company_schema||'FO_LEGAL_ENTITY_T where internal_code =:1' INTO territoryId USING legalEntity;
          dbms_output.Put_line('Territory Id '|| territoryId);
          execute immediate 'select territory_name from '||c_schema.company_schema||'Territory where territory_id = :1 ' INTO country USING territoryId;
          dbms_output.Put_line('Country '|| country);
          EXCEPTION
          WHEN OTHERS THEN
            IF SQLCODE = -00942 THEN
              CONTINUE;
            END IF;
        end;
        end loop;
        end if;
      close crsr;
          EXCEPTION
          WHEN OTHERS THEN
            IF SQLCODE = -00942 THEN
              CONTINUE;
            END IF;
      END;
      END LOOP;
    END;

1 个答案:

答案 0 :(得分:0)

  

将控制台输出数据从pl / sql

插入临时表

您在控制台上看到的输出来自DBMS_OUTPUT.PUT_LINE程序。如果你想将相同的输出插入表中以便进一步处理,我建议使用全局临时表

全局临时表中的数据是私有的,因此会话插入的数据只能由该会话访问。可以为整个会话保留全局临时表中的特定于会话的行,或仅保留当前事务。 ON COMMIT子句表示应在事务结束时删除或保留数据。

  • ON COMMIT DELETE ROWS
  • ON COMMIT PRESERVE ROWS

在您的情况下,创建 GTT 一次,然后在 PL / SQL 代码中,将行插入GTT。

例如,

INSERT INTO company_gtt
   VALUES
(c_schema.company_schema, personId, bioBirthName, personalBirthName ....)

在此处查看更多示例http://oracle-base.com/articles/misc/temporary-tables.php