0

我有两个插入语句,如下所示 A 和 B。我需要将这两个插入语句合并为一个,如语句 C。我不喜欢重复的代码,例如语句 A 和 B 中的选择部分。语句的正确格式是什么C?请忽略动态查询语句,因为真正的语句选择部分非常复杂。

声明 A:

insert into a
  (firstname, lastname)
  select t2.firstname, t3.lastname
    from t1, t2, t3
   where t1.t1_id = t2.t1_id
     and t2.t2_id = t3.t2_id;

声明 B:

insert into b
  (personid)
  select t1.personid
    from t1, t2, t3
   where t1.t1_id = t2.t1_id
     and t2.t2_id = t3.t2_id;

声明 C:

insert into a
  (firstname, lastname)
insert into b
  (personid)
  select t2.firstname, t3.lastname, t1.personid
    from t1, t2, t3
   where t1.t1_id = t2.t1_id
     and t2.t2_id = t3.t2_id;

现实世界的原始陈述如下:

procedure allo_recipient(p_all_id       number,
                         p_allo_type    varchar2,
                         p_step_no      number,
                         p_region       varchar2,
                         p_don_reg_code varchar2,
                         p_med_urgent   varchar2,
                         p_don_id       number,
                         p_abo_ic       varchar2,
                         p_multi_organ  varchar2) is
begin
  insert into all_pat_list
    (apl_id,
     all_id,
     prod_id,
     prior_seq_no,
     org_code,
     all_step_no,
     pat_id,
     pat_name,
     sex,
     dob,
     ped_ind,
     blood_typ,
     home_prov_code,
     home_town,
     org_code_registered,
     org_descrip_registered,
     hosp_id,
     mrn,
     oldr_code,
     current_wait_list_stat_code,
     init_wait_list_stat_date,
     dis_code_prim_diag,
     dis_code_sec_diag,
     cadaver_liv_ind,
     weight,
     height,
     abdom_girth,
     chest_circum,
     chest_vert_right,
     chest_vert_left,
     chest_transverse,
     cmv,
     ebv,
     hepatitis_b_surface_ant,
     hepatitis_c,
     prev_trnsplt_no,
     all_step_descrip,
     eff_date,
     hcv_rna,
     dsa_cur_unaccp1,
     dsa_acc_unaccp1,
     dsa_cur_indet1,
     dsa_acc_indet1,
     dsa_cur_unaccp2,
     dsa_acc_unaccp2,
     dsa_cur_indet2,
     dsa_acc_indet2,
     x_match_serum_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     x_match_b_cell_res_ind, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     x_match_t_cell_res_ind, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     current_pra, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     current_pra_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     peak_pra, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     peak_pra_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     kp_allocation_points,
     cpra) --to do: c_pra need be replaced with new one in furture.
  --3rd select: get all needed elements for all_pat_list
    select apl_seq.nextval apl_id,
           p_all_id all_id,
           prod_id,
           all_kp_prior_seq.nextval prior_seq_no,
           org_code,
           all_step_no,
           pat_id,
           pat_name,
           sex,
           dob,
           ped_ind,
           blood_typ,
           home_prov_code,
           home_town,
           org_code_registered,
           tttt_gen_pkg.get_org_type_desc(org_code_registered) org_descrip_registered,
           hosp_id,
           mrn,
           tttt_gen_pkg.get_hosp_oldr_code(hosp_id) oldr_code,
           current_wait_list_stat_code,
           list_date init_wait_list_stat_date,
           dis_code_prim_diag,
           dis_code_sec_diag,
           cadaver_liv_ind,
           tttt_gen_pkg.get_latest_pat_weight(pat_id) weight,
           tttt_gen_pkg.get_latest_pat_height(pat_id) height,
           tttt_gen_pkg.get_latest_pat_abdom_girth(pat_id) abdom_girth,
           tttt_gen_pkg.get_latest_pat_chest_circum(pat_id) chest_circum,
           tttt_gen_pkg.get_latest_pat_chest_vr(pat_id) chest_vert_right,
           tttt_gen_pkg.get_latest_pat_chest_vl(pat_id) chest_vert_left,
           tttt_gen_pkg.get_latest_pat_chest_trans(pat_id) chest_transverse,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'CMV') cmv,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'EBV') ebv,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HBSAG') hepatitis_b_surface_ant,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HCV') hepatitis_c,
           tttt_gen_pkg.get_prev_tx_count(pat_id, org_code) prev_trnsplt_no, --tttt_gen_pkg.get_prev_tx_count(pat_id, 1) prev_trnsplt_no,
           tttt_gen_pkg.get_all_step_descrip(p_step_no, org_code) all_step_descrip, --tttt_gen_pkg.get_all_step_descrip(p_step_no, 1) all_step_descrip,
           sysdate,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HCV_RNA') hcv_rna,
           null dsa_cur_unaccp1,
           null dsa_acc_unaccp1,
           null dsa_cur_indet1,
           null dsa_acc_indet1,
           null dsa_cur_unaccp2,
           null dsa_acc_unaccp2,
           null dsa_cur_indet2,
           null dsa_acc_indet2,
           null x_match_serum_date,
           null x_match_b_cell_res_ind,
           null x_match_t_cell_res_ind,
           null current_pra,
           null current_pra_date,
           null peak_pra,
           null peak_pra_date,
           kp_allocation_points,
           cpra
      from (
            -- 1st select: get basic elements of pat list for each step
            select distinct prod.org_code,
                             p_step_no all_step_no,
                             p.pat_id,
                             p.last_name || ', ' || p.first_name || ' ' ||
                             p.middle_name pat_name,
                             p.sex,
                             p.dob,
                             decode(sign(months_between(sysdate, p.dob) / 12 - 18), -1, 'P', 'A') ped_ind,
                             p.blood_typ,
                             pr.prov_code_home home_prov_code,
                             pr.home_town,
                             pr.org_code org_code_registered,
                             pr.hosp_id,
                             pr.mrn,
                             tttt_gen_pkg.get_current_wls_code(prod_id) current_wait_list_stat_code,
                             tttt_gen_pkg.get_initial_wls_date(prod.prod_id) list_date, --init_wait_list_stat_date
                             dis_code_prim_diag,
                             dis_code_sec_diag,
                             cadaver_liv_ind,
                             pr.patr_id,
                             prod.prod_id,
                             tttt_kp_allocation.allocation_points(pr.patr_id, prod.org_code) kp_allocation_points,
                             null cpra
              from pat p, temp_kp_pat_pool pr, pat_register_org_det prod
             where p.pat_id = pr.pat_id
               and pr.patr_id = prod.patr_id
               and (p_allo_type, prod.org_code) in
                   (('K', '3'), ('K', '6'), ('P', '6')) -- ('K' => K; K/P; P) ('P' => K/P; P )

               and (pr.patr_id not in
                   (select patr_id from temp_kp_used_patrid_pool)) -- filter out patr_id(s) which has been handled in previous steps

               and pr.cadaver_liv_ind in ('B', 'D') --waiting for deceased and both.

               and (((p_region = 'L') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON') and
                   (tttt_gen_pkg.get_hosp_oldr_code(pr.hosp_id) =
                   p_don_reg_code)) or

                   ((p_region = 'O') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON') and
                   (tttt_gen_pkg.get_hosp_oldr_code(pr.hosp_id) !=
                   p_don_reg_code)) or

                   ((p_region = 'P') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON')) or

                   --((p_region = 'H') and
                   --(tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) != 'ON') and
                   --(tttt_gen_pkg.is_hsp_pat = 'Y')) or -- to do: need pat HSP flag to identify pat belongs to HSP

                   ((p_region = 'NA') and (1 = 1)))

               and (((p_med_urgent = 'Y') and
                   (tttt_gen_pkg.get_current_wls_code(prod.prod_id) = 'H')) or

                   ((p_med_urgent = 'NA') and (1 = 1)))

               and (((p_abo_ic = 'PI') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'PI')) or

                   ((p_abo_ic = 'I') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'I')) or

                   ((p_abo_ic = 'C') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'C')) or

                   ((p_abo_ic = 'NA') and (1 = 1)))

               and (((p_multi_organ = 'Y') and
                   (pr.org_code in (10, 14, 15))) or

                   ((p_multi_organ = 'NA') and (1 = 1)))

             order by kp_allocation_points desc, cpra desc, list_date -- end of 1st select
            ); --end of 2rd select

使用“Ed Gibbs”解决方案的真实世界修改语句如下所示,但不幸的是我得到了编译错误:

“PACKAGE BODY tttt.tttt_KP_ALLOCATION 的编译错误

错误:PL/SQL:ORA-02287:此处不允许序列号行:636 文本:all_kp_prior_seq.nextval prior_seq_no,

错误:PL/SQL:忽略 SQL 语句行:574 文本:将全部插入到 all_pat_list 值中

错误:提示:参数 'p_loc_pro' 已声明但从未在 'run_kp_allo' 中使用 行:847 文本:p_loc_pro varchar2,“

procedure allo_recipient(p_all_id       number,
                         p_allo_type    varchar2,
                         p_step_no      number,
                         p_region       varchar2,
                         p_don_reg_code varchar2,
                         p_med_urgent   varchar2,
                         p_don_id       number,
                         p_abo_ic       varchar2,
                         p_multi_organ  varchar2) is
begin
  insert all into all_pat_list values
    (apl_id,
     all_id,
     prod_id,
     prior_seq_no,
     org_code,
     all_step_no,
     pat_id,
     pat_name,
     sex,
     dob,
     ped_ind,
     blood_typ,
     home_prov_code,
     home_town,
     org_code_registered,
     org_descrip_registered,
     hosp_id,
     mrn,
     oldr_code,
     current_wait_list_stat_code,
     init_wait_list_stat_date,
     dis_code_prim_diag,
     dis_code_sec_diag,
     cadaver_liv_ind,
     weight,
     height,
     abdom_girth,
     chest_circum,
     chest_vert_right,
     chest_vert_left,
     chest_transverse,
     cmv,
     ebv,
     hepatitis_b_surface_ant,
     hepatitis_c,
     prev_trnsplt_no,
     all_step_descrip,
     eff_date,
     hcv_rna,
     dsa_cur_unaccp1,
     dsa_acc_unaccp1,
     dsa_cur_indet1,
     dsa_acc_indet1,
     dsa_cur_unaccp2,
     dsa_acc_unaccp2,
     dsa_cur_indet2,
     dsa_acc_indet2,
     x_match_serum_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     x_match_b_cell_res_ind, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     x_match_t_cell_res_ind, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     current_pra, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     current_pra_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     peak_pra, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     peak_pra_date, -- to do: this field is use by old allocation report, it need determine keep or discard in the new report
     kp_allocation_points,
     cpra) --to do: c_pra need be replaced with new one in furture.
     into temp_kp_used_patrid_pool values (patr_id)
  --3rd select: get all needed elements for all_pat_list
    select apl_seq.nextval apl_id,
           p_all_id all_id,
           prod_id,
           all_kp_prior_seq.nextval prior_seq_no,
           org_code,
           all_step_no,
           pat_id,
           pat_name,
           sex,
           dob,
           ped_ind,
           blood_typ,
           home_prov_code,
           home_town,
           org_code_registered,
           tttt_gen_pkg.get_org_type_desc(org_code_registered) org_descrip_registered,
           hosp_id,
           mrn,
           tttt_gen_pkg.get_hosp_oldr_code(hosp_id) oldr_code,
           current_wait_list_stat_code,
           list_date init_wait_list_stat_date,
           dis_code_prim_diag,
           dis_code_sec_diag,
           cadaver_liv_ind,
           tttt_gen_pkg.get_latest_pat_weight(pat_id) weight,
           tttt_gen_pkg.get_latest_pat_height(pat_id) height,
           tttt_gen_pkg.get_latest_pat_abdom_girth(pat_id) abdom_girth,
           tttt_gen_pkg.get_latest_pat_chest_circum(pat_id) chest_circum,
           tttt_gen_pkg.get_latest_pat_chest_vr(pat_id) chest_vert_right,
           tttt_gen_pkg.get_latest_pat_chest_vl(pat_id) chest_vert_left,
           tttt_gen_pkg.get_latest_pat_chest_trans(pat_id) chest_transverse,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'CMV') cmv,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'EBV') ebv,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HBSAG') hepatitis_b_surface_ant,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HCV') hepatitis_c,
           tttt_gen_pkg.get_prev_tx_count(pat_id, org_code) prev_trnsplt_no, --tttt_gen_pkg.get_prev_tx_count(pat_id, 1) prev_trnsplt_no,
           tttt_gen_pkg.get_all_step_descrip(p_step_no, org_code) all_step_descrip, --tttt_gen_pkg.get_all_step_descrip(p_step_no, 1) all_step_descrip,
           sysdate,
           tttt_gen_pkg.get_latest_test_res(pat_id, 'HCV_RNA') hcv_rna,
           null dsa_cur_unaccp1,
           null dsa_acc_unaccp1,
           null dsa_cur_indet1,
           null dsa_acc_indet1,
           null dsa_cur_unaccp2,
           null dsa_acc_unaccp2,
           null dsa_cur_indet2,
           null dsa_acc_indet2,
           null x_match_serum_date,
           null x_match_b_cell_res_ind,
           null x_match_t_cell_res_ind,
           null current_pra,
           null current_pra_date,
           null peak_pra,
           null peak_pra_date,
           kp_allocation_points,
           cpra,
           patr_id
      from (
            -- 1st select: get basic elements of pat list for each step
            select distinct prod.org_code,
                             p_step_no all_step_no,
                             p.pat_id,
                             p.last_name || ', ' || p.first_name || ' ' ||
                             p.middle_name pat_name,
                             p.sex,
                             p.dob,
                             decode(sign(months_between(sysdate, p.dob) / 12 - 18), -1, 'P', 'A') ped_ind,
                             p.blood_typ,
                             pr.prov_code_home home_prov_code,
                             pr.home_town,
                             pr.org_code org_code_registered,
                             pr.hosp_id,
                             pr.mrn,
                             tttt_gen_pkg.get_current_wls_code(prod_id) current_wait_list_stat_code,
                             tttt_gen_pkg.get_initial_wls_date(prod.prod_id) list_date, --init_wait_list_stat_date
                             dis_code_prim_diag,
                             dis_code_sec_diag,
                             cadaver_liv_ind,
                             pr.patr_id,
                             prod.prod_id,
                             tttt_kp_allocation.allocation_points(pr.patr_id, prod.org_code) kp_allocation_points,
                             null cpra
              from pat p, temp_kp_pat_pool pr, pat_register_org_det prod
             where p.pat_id = pr.pat_id
               and pr.patr_id = prod.patr_id
               and (p_allo_type, prod.org_code) in
                   (('K', '3'), ('K', '6'), ('P', '6')) -- ('K' => K; K/P; P) ('P' => K/P; P )

               and (pr.patr_id not in
                   (select patr_id from temp_kp_used_patrid_pool)) -- filter out patr_id(s) which has been handled in previous steps

               and pr.cadaver_liv_ind in ('B', 'D') --waiting for deceased and both.

               and (((p_region = 'L') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON') and
                   (tttt_gen_pkg.get_hosp_oldr_code(pr.hosp_id) =
                   p_don_reg_code)) or

                   ((p_region = 'O') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON') and
                   (tttt_gen_pkg.get_hosp_oldr_code(pr.hosp_id) !=
                   p_don_reg_code)) or

                   ((p_region = 'P') and
                   (tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) = 'ON')) or

                   --((p_region = 'H') and
                   --(tttt_gen_pkg.get_hosp_prov_code(pr.hosp_id) != 'ON') and
                   --(tttt_gen_pkg.is_hsp_pat = 'Y')) or -- to do: need pat HSP flag to identify pat belongs to HSP

                   ((p_region = 'NA') and (1 = 1)))

               and (((p_med_urgent = 'Y') and
                   (tttt_gen_pkg.get_current_wls_code(prod.prod_id) = 'H')) or

                   ((p_med_urgent = 'NA') and (1 = 1)))

               and (((p_abo_ic = 'PI') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'PI')) or

                   ((p_abo_ic = 'I') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'I')) or

                   ((p_abo_ic = 'C') and
                   (tttt_kp_allocation.kp_abo_c_i(p_don_id, pr.pat_id) = 'C')) or

                   ((p_abo_ic = 'NA') and (1 = 1)))

               and (((p_multi_organ = 'Y') and
                   (pr.org_code in (10, 14, 15))) or

                   ((p_multi_organ = 'NA') and (1 = 1)))

             order by kp_allocation_points desc, cpra desc, list_date -- end of 1st select
            ); --end of 2rd select
4

1 回答 1

6

正如塞巴斯上面所说,INSERT ALL就是答案。您合并的“语句 A”和“语句 B”INSERT ALL将如下所示:

INSERT ALL
  INTO a VALUES (firstname, lastname)
  INTO b VALUES (personid)
select t2.firstname, t3.lastname, t1.personid
  from t1, t2, t3
 where t1.t1_id = t2.t1_id
   and t2.t2_id = t3.t2_id

此外,正如 HLGEM 指出的那样,隐式连接现在通常不受欢迎。以下是使用 ANSI 样式连接的方法:

INSERT ALL
  INTO a VALUES (firstname, lastname)
  INTO b VALUES (personid)
SELECT t2.firstname, t3.lastname, t1.personid
FROM t1
INNER JOIN t2 ON t1.t1_id = t2.t1_id
INNER JOIN t3 ON t2.t2_id = t3.t2_id
于 2013-04-12T20:32:30.863 回答