1

如何根据oracle中的用户输入动态添加列?

我正在根据 from_date 到 to_date 生成月度报告,下面是我的需求示例表

员工代码| 姓名 | CL_TAKEN_DATE | CL_BALANCE | 01-OCT-12 | 2012 年 10 月 2 日 | 03-OCT-12


100001.......................约翰............02-OCT-12...... ......6

100001........克里斯............01-OCT-12............ .....4


根据用户输入,即如果用户需要从 01-OCT-12 到 03-OCT-12 的报告,我需要将该日期作为列添加到我的表中,例如 01-OCT-12 | 2012 年 10 月 2 日 | 03-OCT-12....

下面是我的代码

create or replace
procedure MONTHLY_LVE_NEW_REPORT_demo
(
L_BUSINESS_UNIT                 IN SSHRMS_LEAVE_REQUEST_TRN.BUSINESS_UNIT%TYPE,
--L_LEAVE_TYPE_CODE           IN SSHRMS_LEAVE_REQUEST_TRN.LEAVE_TYPE_CODE%TYPE,
L_DEPARTMENT_CODE           IN VARCHAR2,
--L_MONTH                    IN SSHRMS_LEAVE_REQUEST_TRN.LVE_FROM_DATE%TYPE,
L_FROM_DATE                   IN SSHRMS_LEAVE_REQUEST_TRN.LVE_FROM_DATE%TYPE,
L_TO_DATE                   in SSHRMS_LEAVE_REQUEST_TRN.LVE_TO_DATE%type,
MONTHRPT_CURSOR               OUT        SYS_REFCURSOR
)

 AS
 O_MONTHRPT_CURSOR_RPT clob;
v_return_msg clob;
   BEGIN

   IF (L_BUSINESS_UNIT IS NOT NULL
         AND L_FROM_DATE IS NOT NULL
      and L_TO_DATE is not null
    -- AND L_DEPARTMENT_CODE IS NOT NULL
     )
   THEN

   OPEN MONTHRPT_CURSOR FOR

select EMPLOYEE_CODE, EMPLOYEE_NAME AS NAME, DEPARTMENT_CODE AS DEPARTMENT,DEPARTMENT_DESC, CREATED_DATE,

  NVL(WM_CONCAT(CL_RANGE),'') as CL_TAKEN_DATE,    

  case when NVL(SUM(CL2),0)<0 then 0 else (NVL(SUM(CL2),0)) end as CL_BALANCE,

from
(     
SELECT DISTINCT a.employee_code,
    a.EMPLOYEE_FIRST_NAME || ' ' || a.EMPLOYEE_LAST_NAME as EMPLOYEE_NAME,
    a.DEPARTMENT_CODE,
    a.DEPARTMENT_DESC,
    B.LEAVE_TYPE_CODE,
    B.LVE_UNITS_APPLIED,
    B.CREATED_DATE as CREATED_DATE,

    DECODE(b.leave_type_code,'CL',SSHRMS_LVE_BUSINESSDAY(L_BUSINESS_UNIT,to_char(b.lve_from_date,'mm/dd/yyyy'), to_char(b.lve_to_date,'mm/dd/yyyy'))) CL_RANGE,

    DECODE(B.LEAVE_TYPE_CODE,'CL',B.LVE_UNITS_APPLIED)CL1,
    b.status

from SSHRMS_EMPLOYEE_DATA a 
    join 
    SSHRMS_LEAVE_BALANCE C 
    on a.EMPLOYEE_CODE = C.EMPLOYEE_CODE
     and  C.STATUS = 'Y'
    left join 
    SSHRMS_LEAVE_REQUEST_TRN B
    on
    B.EMPLOYEE_CODE=C.EMPLOYEE_CODE  
    and c.EMPLOYEE_CODE = b.EMPLOYEE_CODE
    and B.LEAVE_TYPE_CODE = C.LEAVE_TYPE_CODE
    and B.STATUS in ('A','P','C')  
    and (B.LVE_FROM_DATE >= TO_DATE(L_FROM_DATE, 'DD/MON/RRRR')
    and B.LVE_TO_DATE <= TO_DATE(L_TO_DATE, 'DD/MON/RRRR'))
    join
    SSHRMS_LEAVE_REQUEST_TRN D
    on a.EMPLOYEE_CODE = D.EMPLOYEE_CODE
    and D.LEAVE_TYPE_CODE in ('CL')
    AND D.LEAVE_TYPE_CODE IS NOT NULL

)

group by EMPLOYEE_CODE, EMPLOYEE_NAME, DEPARTMENT_CODE, DEPARTMENT_DESC, CREATED_DATE
;
else
v_return_msg:='Field should not be empty';
 end if;
END;

我的代码实际输出


员工代码| 姓名 | CL_TAKEN_DATE | CL_BALANCE

100001.......................约翰............02-OCT-12...... ......6

100001........克里斯............01-OCT-12............ .....4


如何根据from_date动态添加列到to_date?

谢谢并恭祝安康,

克里斯·杰罗姆。

4

1 回答 1

0

线索在问题中:

“如何在 oracle 中根据用户输入动态添加列?”

使用动态 SQL。 了解更多

于 2012-11-01T08:25:57.767 回答