Oracle存储过程--工作实战
这是我上一家公司工作中写的存过
--==========================================================
--procedure name : settle_submit_check
--description : 结清提交校验
--note :
--parameter :
--==========================================================
PROCEDURE settle_submit_check
(
p_settle_type VARCHAR2,
p_case_id NUMBER,
p_periods NUMBER,
p_user_id NUMBER
) IS
v_exists_not_w_off VARCHAR2(1);
v_count NUMBER;
v_repayment_mode afd_case.repayment_mode%TYPE;
v_peroid_type afd_case_business_type.peroid_type%TYPE;
e_no_settle_err EXCEPTION;
e_settle_normal_err EXCEPTION;
e_settle_unnormal_err EXCEPTION;
BEGIN
SELECT COUNT(1)
INTO v_count
FROM csh_payment_req_doc c
WHERE c.case_id = p_case_id
AND c.ref_v01 = 'REFUND'
AND c.status NOT IN ('CANCEL',
'REFUSE',
'CLOSE',
'FULL_PAY');
IF v_count > 0 THEN
RAISE e_no_settle_err;
END IF;
SELECT c.repayment_mode,
(SELECT t.peroid_type FROM afd_case_business_type t WHERE t.business_type_id = c.business_type_id)
INTO v_repayment_mode,
v_peroid_type
FROM afd_case c
WHERE c.case_id = p_case_id;
v_exists_not_w_off := 'N';
IF v_peroid_type <> 'SHORT_RENT' OR (v_peroid_type = 'SHORT_RENT' AND v_repayment_mode = '10') THEN
BEGIN
SELECT 'Y'
INTO v_exists_not_w_off
FROM dual
WHERE EXISTS (SELECT 1
FROM afd_cashflow f,
afd_cashflow_type t
WHERE f.case_id = p_case_id
AND f.wirte_off_status = 'NOT_WRITE_OFF'
AND f.direction = 'INFLOW'
AND f.cashflow_type_id = t.cashflow_type_id
AND t.cashflow_type_code <> '24'
AND f.period > 0
AND f.period <= p_periods)
OR EXISTS (SELECT 1
FROM afd_cashflow f,
afd_cashflow_type t
WHERE f.case_id = p_case_id
AND f.wirte_off_status <> 'NOT_WRITE_OFF'
AND f.direction = 'INFLOW'
AND f.cashflow_type_id = t.cashflow_type_id
AND t.cashflow_type_code <> '24'
AND f.amount - nvl(f.received_amount,
0) > g_receive_period_dif_amount
AND f.period > 0
AND f.period <= p_periods);
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_exists_not_w_off := 'N';
END;
ELSE
--先息后本,后付的判断最后一期前几期是否存在未核销或核销金额差100的
BEGIN
SELECT 'Y'
INTO v_exists_not_w_off
FROM dual
WHERE EXISTS (SELECT 1
FROM afd_cashflow f,
afd_cashflow_type t
WHERE f.case_id = p_case_id
AND t.cashflow_type_id = f.cashflow_type_id
AND t.cashflow_type_code NOT IN ('24',
'25')
AND f.wirte_off_status <> 'COMPLETE_WRITE_OFF'
AND f.direction = 'INFLOW'
AND f.amount - nvl(f.received_amount,
0) > g_receive_period_dif_amount
AND f.period > 0
AND f.period <= p_periods);
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_exists_not_w_off := 'N';
END;
END IF;
IF p_settle_type = g_settle_type_normal AND v_exists_not_w_off = 'Y' THEN
RAISE e_settle_normal_err;
END IF;
IF p_settle_type IN (g_settle_type_early,
g_settle_type_unpreservation) AND v_exists_not_w_off = 'N' THEN
RAISE e_settle_unnormal_err;
END IF;
EXCEPTION
WHEN e_settle_normal_err THEN
sys_raise_app_error_pkg.raise_user_define_error(p_message_code => 'SETTLE_NORMAL_CANNOT_ERROR',
p_created_by => p_user_id,
p_package_name => c_pkg_name,
p_procedure_function_name => 'SETTLE_SUBMIT_CHECK');
raise_application_error(sys_raise_app_error_pkg.c_error_number,
sys_raise_app_error_pkg.g_err_line_id);
WHEN e_settle_unnormal_err THEN
sys_raise_app_error_pkg.raise_user_define_error(p_message_code => 'SETTLE_UNNORMAL_CANNOT_ERROR',
p_created_by => p_user_id,
p_package_name => c_pkg_name,
p_procedure_function_name => 'SETTLE_SUBMIT_CHECK');
raise_application_error(sys_raise_app_error_pkg.c_error_number,
sys_raise_app_error_pkg.g_err_line_id);
WHEN e_no_settle_err THEN
sys_raise_app_error_pkg.raise_sys_others_error(p_message => '这笔案件存在未处理的押金退款申请,请处理后再申请结清',
p_created_by => p_user_id,
p_package_name => c_pkg_name,
p_procedure_function_name => 'SETTLE_SUBMIT_CHECK');
raise_application_error(sys_raise_app_error_pkg.c_error_number,
sys_raise_app_error_pkg.g_err_line_id);
END settle_submit_check;