pl/sql 노트

1. select에서 나온 문자열형 데이터는 상량 문자열을 비교하기 전에 관련 처리만 해야 한다. 예를 들어 (trim 등)
다음과 같습니다.
for per_msg_rec in per_msg_cur(pf_pri_rec.portfolio_seq,
                               I_TRANS_DATE,
                               per_security_rec.security_id) loop
                    case trim(per_msg_rec.ivt)
                      when 'IVT_BO' then
                        IVT_BO_VAR := per_msg_rec.mount;
                      
                      when 'IVT_CR_B' then
                        IVT_CR_B_VAR := per_msg_rec.mount;
                    end case; 

커서의 ivt를 처리하지 않으면 전체case의 데이터와 일치하지 않을 수 있습니다.
2. 두cursor를 하나로 통합한다.
예컨대
    cursor per_security_cur1(v_portfolio  inventory_detail.portfolio_seq%TYPE,
                            v_trans_date inventory_detail.trans_date%type,
                            v_security   inventory_detail.security_id%TYPE) is
      select distinct d.security_id
        from inventory_detail d
       where d.trans_date <= v_trans_date
         and d.portfolio_seq = v_portfolio
         and v_security = '-1';

화목하다
    cursor per_security_cur2(y_detail.security_id%TYPE) is
      select v_security
        from dual
       where v_security <> '-1'
       order by security_id;

통합
cursor per_security_cur(v_portfolio  inventory_detail.portfolio_seq%TYPE,
                            v_trans_date inventory_detail.trans_date%type,
                            v_security   inventory_detail.security_id%TYPE) is
      select distinct d.security_id
        from inventory_detail d
       where d.trans_date <= v_trans_date
         and d.portfolio_seq = v_portfolio
         and v_security = '-1'
      union
      select v_security
        from dual
       where v_security <> '-1'
       order by security_id;

 3.단일select 조회 문장에 대해 값 조회를 하기 전에 기록이 존재하는지 확인해야 합니다. 커서의 경우 기록을 찾지 못할 때 이 커서가 종료됩니다.
예:
            select sum(nvl(case d.mult
                             when 1 then
                              d.var_amount
                             when -1 then
                              -d.var_amount
                             else
                              d.var_amount * d.mult
                           end,
                           0))
              into AMOUNT_VAR
              from inventory_detail d
             where d.trans_date <= I_TRANS_DATE
               and d.portfolio_seq = per_list_rec.message
               and d.security_id = per_security_rec.security_id
               and d.ivt in ('IVT_BO', 'IVT_OR_S', 'IVT_CR_S');

조건에 맞는 기록이 없을 때 AMOUNT_VAR 값이 NULL로 설정됩니다.
뒤에 AMOUNT_VAR 작업은 의미가 없습니다.예:
            if (AMOUNT_VAR < 0)
            remain_var := AMOUNT_VAR;

          4. 테이블 백업 sql.
-- Create table
create table INVENTORY_CONTAIN
(
  INV_GRP_SEQ   NUMBER(5) not null,
  PORTFOLIO_SEQ NUMBER(5) not null,
  PRIORITY      NUMBER(3) default 1
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the columns 
comment on column INVENTORY_CONTAIN.INV_GRP_SEQ
  is 'inventory group number, ref cms.inventory_grp,  ';
comment on column INVENTORY_CONTAIN.PORTFOLIO_SEQ
  is 'portfolio number, ref cms.portfolio,  ';
comment on column INVENTORY_CONTAIN.PRIORITY
  is 'priority of portfolio in this inventory group,  ';
-- Create/Recreate primary, unique and foreign key constraints 
alter table INVENTORY_CONTAIN
  add constraint PK_INVENTORY_CONTAIN primary key (INV_GRP_SEQ, PORTFOLIO_SEQ)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
alter table INVENTORY_CONTAIN
  add constraint UQ_INVENTORY_CONTAIN_PORTFOLIO unique (PORTFOLIO_SEQ)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

wms 쿼리 부여 LOGON_SITE 테이블, conterparty_ 실행main의 권한입니다.
grant select on LOGON_SITE to WMS;
grant execute on counterparty_main to wms; 

5. 중첩 테이블 유형
CREATE OR REPLACE TYPE "MSG_ENTRY" AS OBJECT
(
  SEQ           int,
  MESSAGE       VARCHAR2(4000)
)
CREATE OR REPLACE TYPE "MSG_LIST" AS TABLE OF MSG_ENTRY

  FUNCTION QUERY_INV_GRP_PF(
    I_CUS_NUMBER IN WCUSTOMER.CUS_NUMBER%TYPE
    ) return msg_list as
  
    lc_ret msg_list := msg_list();
  
    cursor inv_grp_cur IS
      SELECT INV_GRP_SEQ, INV_GRP_NAME, ALERT_TYPE, STATUS
        FROM inventory_grp t
       WHERE t.cus_number = I_CUS_NUMBER
         and t.status not in ('U', 'D');
 
    procedure add_row(text in varchar2) as
    begin
      lc_ret.extend;
      lc_ret(lc_ret.last) := MSG_ENTRY(lc_ret.last, text);
    end;
  
  begin
    OPEN inv_grp_cur;
  
    add_row('<Page>');
    LOOP
      FETCH inv_grp_cur
        INTO inv_grp_rec;
      EXIT WHEN inv_grp_cur%NOTFOUND;
      add_row(CHR(9) || 
        '<GRP_INFO>' || CHR(10) || CHR(9) || CHR(9) ||
        '<INV_GRP_SEQ>' || inv_grp_rec.inv_grp_seq || '</INV_GRP_SEQ>' || CHR(10) || CHR(9) || CHR(9) ||
        '<INV_GRP_NAME>' || trim(inv_grp_rec.inv_grp_name) || '</INV_GRP_NAME>' || CHR(10) || CHR(9) || CHR(9) ||
        '<ALERT_TYPE>' || inv_grp_rec.alert_type || '</ALERT_TYPE>' || CHR(10) || CHR(9) || CHR(9) || 
        '<STATUS>' || inv_grp_rec.status || '</STATUS>' || CHR(10) || CHR(9) ||
        '</GRP_INFO>');
    END LOOP;
    add_row('</Page>');
    return lc_ret;
  exception
    when others then
      add_row(' .' || CHR(10));
      add_row('</Page>');
      RETURN lc_ret;
  END QUERY_INV_GRP_PF;

실행 sql
select message from table(inventory_control.QUERY_INV_GRP_CUSTOMER(76,'Y'))

결과는 다음과 같다.
<Page>
 <DETAIL>
  <INV_GRP_SEQ>9538</INV_GRP_SEQ>
  <INV_GRP_NAME>dt_1</INV_GRP_NAME>
  <ALERT_TYPE>A</ALERT_TYPE>
  <STATUS>A</STATUS>
  <USER_NUMBER>595</USER_NUMBER>
  <CUS_NUMBER>76</CUS_NUMBER>
  <OPEN_REPO_ENABLE>Y</OPEN_REPO_ENABLE>
  <OPEN_REPO_PRIORITY>1</OPEN_REPO_PRIORITY>
 </DETAIL>"
 <DETAIL>
  <INV_GRP_SEQ>9539</INV_GRP_SEQ>
  <INV_GRP_NAME>dt_2</INV_GRP_NAME>
  <ALERT_TYPE>N</ALERT_TYPE>
  <STATUS>A</STATUS>
  <USER_NUMBER>595</USER_NUMBER>
  <CUS_NUMBER>76</CUS_NUMBER>
  <OPEN_REPO_ENABLE>N</OPEN_REPO_ENABLE>
  <OPEN_REPO_PRIORITY>1</OPEN_REPO_PRIORITY>
 </DETAIL>
</Page>

좋은 웹페이지 즐겨찾기