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

时间:2012-11-01 06:58:48

标签: oracle add

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

我根据from_date到to_date生成月度报告,下面是我的要求样本表

EMPLOYEE_CODE |名称| CL_TAKEN_DATE | CL_BALANCE | 01-OCT-12 | 02-OCT-12 |的 03-OCT-12


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

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


根据用户输入,即如果用户需要01-OCT-12到03-OCT-12的报告,我需要在表格中添加日期作为列,如01-OCT-12 | 02-OCT-12 | 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;

我的代码实际输出


EMPLOYEE_CODE |名称| CL_TAKEN_DATE | CL_BALANCE

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

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


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

谢谢和问候,

Chris Jerome。

1 个答案:

答案 0 :(得分:0)

问题在于:

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

使用动态SQL。 {{3P>