저장 프로시저 2

10688 단어 저장 프로세스
create or replace procedure bps_special_marketing(TABYEAR in number, SYSDEPTCODE in char, DEPTCODE in char) is
v_item_code number(20) default 1;
v_id number(20) default 10; --DXP_29   id 
v_sort_no number(6) default 10;--DXP_29   sort_no ,      

v_item_name varchar2(200);--   ,    
v_project_id varchar2(255);--  id
v_project_name varchar2(255);--    
v_category_name varchar2(255);--    
v_impl_dept varchar2(255);--    
v_build_volumn varchar2(255);--    
--      (0:    ,1:    ,2:    ,3:    ,4:    ,5:    ,6:    ,7:    )
v_init_status number(10);--       
v_init_status_show varchar2(20); --        ,
--    (0:   ;1:   )
v_issue_status number(10);--       
v_issue_status_show varchar2(10);--        ,
v_source_name varchar2(255);--    
v_start_date date;
v_finish_date date;
v_total_invest varchar2(255);--   
v_continued number(10);--    ,  project   INTEGRATIVE_PLAN_ISSUED_STATUS  ,   (1)    ,   (0)    
v_invest_amount number(19,2) default 0;--     
v_exec_amount number(19,2) default 0;--     

--           
v_second_id varchar2(255);--    id
v_second_name varchar2(255);--      
cursor v_second_category_record is select a.id, a.category_name
							from bps_project_category a
						where a.category_level=2
							and a.father_code_id=(select distinct id
														from bps_project_category
													where category_name='    ' and category_level=1);
v_dept_record report_package.bps_cursor;--     ,                  
v_dept_id varchar2(255);
v_dept_name varchar2(255);

v_project_record report_package.bps_cursor;--     ,                   

--         
v_dept_total_invest varchar2(255);--       


begin

delete from DXP_29 where tab_year=TABYEAR and dept_code = DEPTCODE and sys_dept_code=SYSDEPTCODE;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line(TABYEAR||','||DEPTCODE||','||SYSDEPTCODE);
commit;

--      
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values('  ', v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
for i in 1..2 loop
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
		values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;


open v_second_category_record;
loop
fetch v_second_category_record into v_second_id, v_second_name;
exit when v_second_category_record%notfound;
--        
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values(v_second_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;
close v_second_category_record;

--           
open v_second_category_record;
loop
fetch v_second_category_record into v_second_id, v_second_name;--    
exit when v_second_category_record%notfound;
--                
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
		values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
--        ,         ,                      
insert into DXP_29(item_code, item_name, id, dept_code, sys_dept_code, tab_year, sort_no)
	values(v_item_code, v_second_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_item_code:=v_item_code+1;
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;

--       ,                 ,                 ,   ,         ,          
--1,            ,        ,          
open v_dept_record for select distinct a.bps_department_id,
								c.department_name
						from bps_project a,
							bps_project_planning b,
							bps_department c
					where a.bps_department_id is not null
						and a.bps_department_id = c.id
						and b.project_id = a.id
						and b.version_active_status = 1
						and b.fiscal_year = TABYEAR
						and (a.bps_category_id=v_second_id
								or a.bps_category_id in (select b.id
															from bps_project_category b
														where b.father_code_id=v_second_id));
loop
fetch v_dept_record into v_dept_id, v_dept_name;
exit when v_dept_record%notfound;
--           ,             id
--                
insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no)
			values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
insert into DXP_29(item_name, id, dept_code, sys_dept_code, tab_year, sort_no, second_id)
		values(v_dept_name, v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no, v_second_id);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;

--                   
open v_project_record for select distinct b.id,
								b.project_name,
						(select department_name from bps_department where id=b.bps_impl_dept_id) bps_impl_dept_name,
						b.total_volumn,
						b.project_initialize_status,
						c.issue_status,
						(select source_name from bps_investment_source, bps_prj_inves_relations
							where bps_prj_inves_relations.bps_project_id=b.id
								and bps_investment_source.id = bps_prj_inves_relations.bps_investment_source_id) source_name,
						b.start_date,
						b.finish_date,
						b.total_investment,
						b.integrative_plan_issued_status,
						c.adjust_planning_amount,
								c.adjust_execution_amount
							from bps_department       a,
						bps_project          b,
						bps_project_planning c
					 where b.bps_department_id = v_dept_id	--    
						and b.bps_department_id = a.id
						and c.project_id = b.id
						and c.version_active_status = 1
						and c.fiscal_year = TABYEAR
						and (b.bps_category_id = v_second_id
								or b.bps_category_id in(select e.id from bps_project_category e where e.father_code_id=v_second_id)
							);
loop
fetch v_project_record into v_project_id, v_project_name, v_impl_dept
				, v_build_volumn, v_init_status, v_issue_status, v_source_name, v_start_date
				, v_finish_date, v_total_invest, v_continued, v_invest_amount, v_exec_amount;
exit when v_project_record%notfound;
--              ,     ,     id                     

--      (0:    ,1:    ,2:    ,3:    ,4:    ,5:    ,6:    ,7:    )
if v_init_status=0 then
	v_init_status_show:='    ';
elsif v_init_status=1 then
	v_init_status_show:='    ';
elsif v_init_status=2 then
	v_init_status_show:='    ';
elsif v_init_status=3 then
	v_init_status_show:='    ';
elsif v_init_status=4 then
	v_init_status_show:='    ';
elsif v_init_status=5 then
	v_init_status_show:='    ';
elsif v_init_status=6 then
	v_init_status_show:='    ';
elsif v_init_status=7 then
	v_init_status_show:='    ';
else
	v_init_status_show:='';
end if;
--    (0:   ;1:   )
if v_issue_status=0 then
	v_issue_status_show:='   ';
elsif v_issue_status=1 then
	v_issue_status_show:='   ';
else
	v_issue_status_show:='';
end if;

insert into DXP_29(id, dept_code, sys_dept_code, tab_year, sort_no
				, project_name, category_name, impl_dept, build_volumn, init_status, issue_status
				, source_name, start_date, finish_date, total_invest, invest_amount, exec_amount, second_id, dept_id)
			values(v_id, DEPTCODE, SYSDEPTCODE, TABYEAR, v_sort_no
				, v_project_name, v_second_name, v_impl_dept, v_build_volumn, v_init_status_show, v_issue_status_show
				, v_source_name, v_start_date, v_finish_date, v_total_invest, v_invest_amount, v_exec_amount, v_second_id, v_dept_id);
v_id:=v_id+1;
v_sort_no:=v_sort_no+1;
end loop;
close v_project_record;

--    
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
			,invest_amount=(select sum(invest_amount) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
			,exec_amount=(select sum(exec_amount) from DXP_29 where dept_id=v_dept_id and second_id=v_second_id
													and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		where item_name=v_dept_name and second_id=v_second_id
				and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;
end loop;
close v_dept_record;
--      
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		,invest_amount=(select sum(invest_amount) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
		,exec_amount=(select sum(exec_amount) from DXP_29 where second_id=v_second_id and dept_id is null
												and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	where item_name=v_second_name and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;

end loop;
close v_second_category_record;

--  
update DXP_29 set total_invest=(select sum(to_number(total_invest)) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	, invest_amount=(select sum(invest_amount) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
	, exec_amount=(select sum(exec_amount) from DXP_29 where item_code is not null
											and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE)
where item_name='  ' and tab_year=TABYEAR and dept_code=DEPTCODE and sys_dept_code=SYSDEPTCODE;

commit;
end;
/

좋은 웹페이지 즐겨찾기