Oracle SQL:在内部循环中使用外部循环标识符立即执行



我必须执行32次非常相似的操作,即为给定记录(给定季度)设置行中列的值。

为了简化我的代码并实现美观,我想使用带有立即执行的 for 循环,使用 I_cnt 在语句中动态设置列名。

我正在使用甲骨文 10g。

当我调用该过程时,Oracle 返回

SQL 错误: ORA-00904:"REC"."QUARTER_MEL": Niepoprawny identyfikator ORA-06512:przy"LREBIRT。P_PFPL_RISKPROFILE_TEST", 丽尼亚 55 00904. 00000 - "%s: 无效标识符"

当我调用下面的过程时,v_risk_volume和v_risk_amount都计算正确,它无法立即执行我的语句。

我的程序代码:

PROCEDURE CALCULATE_RESULT_TABLE AS
  v_risk_volume float;
  v_risk_amount float;
  v_sql varchar2(1000);
  BEGIN
for rec in (select * from PFPL_RISKPROFILE_BASIS_TEST)
LOOP
  for i_cnt in 1..32
  LOOP
    DBMS_OUTPUT.PUT_LINE(rec.quarter_mel);
    select Q_VOLUME, Q_AMOUNT into v_risk_volume, v_risk_amount from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = i_cnt;
     DBMS_OUTPUT.PUT_LINE(v_risk_volume);
      DBMS_OUTPUT.PUT_LINE(v_risk_amount);
    v_sql := 'update PFPL_RISKPROFILE_RES_TEST t set Q_'||i_cnt||'_volume = v_risk_volume/rec.Q_VOLUME, Q_'||i_cnt||'_amount = v_risk_amount/rec.Q_AMOUNT where t.QUARTER_MEL = rec.QUARTER_MEL';
    DBMS_OUTPUT.PUT_LINE(v_sql);
    EXECUTE IMMEDIATE v_sql;
  END LOOP;
END LOOP;
  END CALCULATE_RESULT_TABLE;

dbms_output的结果:

2012年1月 7 448787,05 更新PFPL_RISKPROFILE_RES_TEST T set Q_1_volume = v_risk_volume/rec。Q_VOLUME,Q_1_amount = v_risk_amount/记录。Q_AMOUNT其中t.QUARTER_MEL = rec。QUARTER_MEL

先前更正后过程的当前版本:

PROCEDURE CALCULATE_RESULT_TABLE AS
  v_risk_volume float;
  v_risk_amount float;
  v_sql varchar2(1000);
  BEGIN
    for rec in (select * from PFPL_RISKPROFILE_BASIS_TEST)
    LOOP
    DBMS_OUTPUT.PUT_LINE(rec.quarter_mel);
      for i_cnt in 1..32
      LOOP
        DBMS_OUTPUT.PUT_LINE(i_cnt);      
    DBMS_OUTPUT.PUT_LINE(rec.quarter_mel);
    select Q_VOLUME, Q_AMOUNT into v_risk_volume, v_risk_amount from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = i_cnt;
    if rec.Q_volume > 0 then v_risk_volume := round(v_risk_volume/rec.Q_volume,4); else v_risk_volume := 0; end if;
    if rec.Q_amount > 0 then v_risk_amount := round(v_risk_amount/rec.Q_amount,4); else v_risk_amount := 0; end if;
    v_sql := 'update PFPL_RISKPROFILE_RES_TEST t set Q_'||i_cnt||'_volume = :v, Q_'||i_cnt||'_amount = :a where t.QUARTER_MEL = :q';
    DBMS_OUTPUT.PUT_LINE(v_sql);      
    EXECUTE IMMEDIATE v_sql USING v_risk_volume, v_risk_amount, rec.quarter_mel;
  END LOOP;
END LOOP;
  END CALCULATE_RESULT_TABLE;

再次你好,我尝试了 Jeff 的建议,但我仍然有一个问题,而且真的很丑,我写代码差点哭了——我做了 10 个季度,我仍然需要粘贴缺失的 22 个季度。现在,该过程处理了循环的四行中的两行。

PROCEDURE CALCULATE_RESULT_TABLE AS
  v_risk_volume_1 FLOAT;
  v_risk_volume_2 FLOAT;
  v_risk_volume_3 FLOAT;
  v_risk_volume_4 FLOAT;
  v_risk_volume_5 FLOAT;
  v_risk_volume_6 FLOAT;
  v_risk_volume_7 FLOAT;
  v_risk_volume_8 FLOAT;
  v_risk_volume_9 FLOAT;
  v_risk_volume_10 FLOAT;
  v_risk_amount_1 FLOAT;
  v_risk_amount_2 FLOAT;
  v_risk_amount_3 FLOAT;
  v_risk_amount_4 FLOAT;
  v_risk_amount_5 FLOAT;
  v_risk_amount_6 FLOAT;
  v_risk_amount_7 FLOAT;
  v_risk_amount_8 FLOAT;
  v_risk_amount_9 FLOAT;
  v_risk_amount_10 FLOAT;
  BEGIN
for rec in (select * from PFPL_RISKPROFILE_BASIS_TEST order by quarter_mel)
LOOP
DBMS_OUTPUT.PUT_LINE(rec.quarter_mel);
select Q_VOLUME, Q_AMOUNT into v_risk_volume_1, v_risk_amount_1 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 1;
    if rec.Q_volume > 0 then v_risk_volume_1 := round(v_risk_volume_1/rec.Q_volume,4); else v_risk_volume_1 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_1 := round(v_risk_amount_1/rec.Q_amount,4); else v_risk_amount_1 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_2, v_risk_amount_2 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 2;
    if rec.Q_volume > 0 then v_risk_volume_2 := round(v_risk_volume_2/rec.Q_volume,4); else v_risk_volume_2 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_2 := round(v_risk_amount_2/rec.Q_amount,4); else v_risk_amount_2 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_3, v_risk_amount_3 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 3;
    if rec.Q_volume > 0 then v_risk_volume_3 := round(v_risk_volume_3/rec.Q_volume,4); else v_risk_volume_3 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_3 := round(v_risk_amount_3/rec.Q_amount,4); else v_risk_amount_3 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_4, v_risk_amount_4 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 4;
    if rec.Q_volume > 0 then v_risk_volume_4 := round(v_risk_volume_4/rec.Q_volume,4); else v_risk_volume_4 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_4 := round(v_risk_amount_4/rec.Q_amount,4); else v_risk_amount_4 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_5, v_risk_amount_5 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 5;
    if rec.Q_volume > 0 then v_risk_volume_5 := round(v_risk_volume_5/rec.Q_volume,4); else v_risk_volume_5 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_5 := round(v_risk_amount_5/rec.Q_amount,4); else v_risk_amount_5 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_6, v_risk_amount_6 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 6;
    if rec.Q_volume > 0 then v_risk_volume_6 := round(v_risk_volume_6/rec.Q_volume,4); else v_risk_volume_6 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_6 := round(v_risk_amount_6/rec.Q_amount,4); else v_risk_amount_6 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_7, v_risk_amount_7 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 7;
    if rec.Q_volume > 0 then v_risk_volume_7 := round(v_risk_volume_7/rec.Q_volume,4); else v_risk_volume_7 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_7 := round(v_risk_amount_7/rec.Q_amount,4); else v_risk_amount_7 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_8, v_risk_amount_8 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 8;
    if rec.Q_volume > 0 then v_risk_volume_8 := round(v_risk_volume_8/rec.Q_volume,4); else v_risk_volume_8 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_8 := round(v_risk_amount_8/rec.Q_amount,4); else v_risk_amount_8 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_9, v_risk_amount_9 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 9;
    if rec.Q_volume > 0 then v_risk_volume_9 := round(v_risk_volume_9/rec.Q_volume,4); else v_risk_volume_9 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_9 := round(v_risk_amount_9/rec.Q_amount,4); else v_risk_amount_9 := 0; end if;
select Q_VOLUME, Q_AMOUNT into v_risk_volume_10, v_risk_amount_10 from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter = 10;
    if rec.Q_volume > 0 then v_risk_volume_10 := round(v_risk_volume_10/rec.Q_volume,4); else v_risk_volume_10 := 0; end if;
    if rec.Q_volume > 0 then v_risk_amount_10 := round(v_risk_amount_10/rec.Q_amount,4); else v_risk_amount_10 := 0; end if;
update PFPL_RISKPROFILE_RES_TEST set 
Q_1_volume = v_risk_volume_1,
Q_2_volume = v_risk_volume_2,
Q_3_volume = v_risk_volume_3,
Q_4_volume = v_risk_volume_4,
Q_5_volume = v_risk_volume_5,
Q_6_volume = v_risk_volume_6,
Q_7_volume = v_risk_volume_7,
Q_8_volume = v_risk_volume_8,
Q_9_volume = v_risk_volume_9,
Q_10_volume = v_risk_volume_10 where quarter_mel = rec.quarter_mel;
update PFPL_RISKPROFILE_RES_TEST set 
Q_1_amount = v_risk_amount_1,
Q_2_amount = v_risk_amount_2,
Q_3_amount = v_risk_amount_3,
Q_4_amount = v_risk_amount_4,
Q_5_amount = v_risk_amount_5,
Q_6_amount = v_risk_amount_6,
Q_7_amount = v_risk_amount_7,
Q_8_amount = v_risk_amount_8,
Q_9_amount = v_risk_amount_9,
Q_10_amount = v_risk_amount_10 where quarter_mel = rec.quarter_mel;
    END LOOP;
  END CALCULATE_RESULT_TABLE;

v_sql := '更新PFPL_RISKPROFILE_RES_TEST t set Q_'||i_cnt||'_volume = v_risk_volume/记录。Q_VOLUME, Q_'||i_cnt||'_amount = v_risk_amount/记录。Q_AMOUNT其中t.QUARTER_MEL = rec。QUARTER_MEL';

您没有在此处传递变量值。您需要在 EXECUTE IMMEDIATE 中使用绑定变量和 USING 子句来引用绑定值。

您需要按以下方式执行此操作:

v_sql := 'update PFPL_RISKPROFILE_RES_TEST t set Q_'||i_cnt||'_volume = :v_risk_volume/rec.Q_VOLUME, Q_'||i_cnt||'_amount = :v_risk_amount/:rec.Q_AMOUNT where t.QUARTER_MEL = rec.QUARTER_MEL';
execute immediate v_sql using v_risk_volume, v_risk_amount, rec.Q_AMOUNT

所以,我终于找到了最后一个错误的原因,我想在这里编译答案的不同元素。

第一个错误是使用 EXECUTE 即时,执行的 sql 不应包含任何引用,我用绑定变量替换,没关系。

第二个错误是,虽然内部光标从 1..32 开始,但对于所有 32 次迭代,不一定有值可供选择v_risk_volume和v_risk_amount,并且该过程在第一次迭代中崩溃,其中选择 ...到。。。什么也没回来。

所以我改变了逻辑,将内部光标放在选择中的表上......到。。。声明,现在它运行完美。我对这个解决方案非常满意,它用最少的代码完成了工作,并且由于处理的最大记录数约为 1024,因此性能是完全可以接受的。

此过程的最终代码:

  PROCEDURE CALCULATE_RESULT_TABLE AS
  v_risk_volume float;
  v_risk_amount float;
  v_i_volume varchar2(30);
  v_i_amount varchar2(30);
  v_sql varchar2(1000);
  BEGIN
    for rec in (select * from PFPL_RISKPROFILE_RES_TEST)
    LOOP
DBMS_OUTPUT.PUT_LINE(rec.quarter_mel);
NULL;
  for i in (select * from PFPL_RISKPROFILE_CDR_Q_TEST where quarter_mel = rec.quarter_mel and quarter between 1 and 32 order by quarter)
  LOOP
    if rec.Q_volume > 0 then v_risk_volume := round(i.Q_VOLUME/rec.Q_volume,4); else v_risk_volume := 0; end if;
    if rec.Q_amount > 0 then v_risk_amount := round(i.Q_AMOUNT/rec.Q_amount,4); else v_risk_amount := 0; end if;
    v_i_volume := 'Q_'||i.quarter||'_volume';
    v_i_amount := 'Q_'||i.quarter||'_amount';
    v_sql := 'update PFPL_RISKPROFILE_RES_TEST t set '||v_i_volume||' = :v, '||v_i_amount||' = :a where t.QUARTER_MEL = :q';
    EXECUTE IMMEDIATE v_sql USING v_risk_volume, v_risk_amount, rec.quarter_mel;
  END LOOP;
END LOOP;
  END CALCULATE_RESULT_TABLE;

相关内容

  • 没有找到相关文章

最新更新