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

时间:2015-03-10 08:28:33

标签: sql for-loop oracle10g execute-immediate

我必须执行32次非常类似的操作,即为给定记录(对于给定的四分之一)设置一行中的列值。

为了简化我的代码并为了美丽而茁壮成长,我想使用带有执行立即的for循环,使用I_cnt在语句中动态设置列名。

我正在使用Oracle 10g。

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

  

SQL错误:ORA-00904:“REC”。“QUARTER_MEL”:niepoprawny identyfikator   ORA-06512:przy“LREBIRT.P_PFPL_RISKPROFILE_TEST”,linia 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:

的结果
  

1-2012   7   448787,05   更新PFPL_RISKPROFILE_RES_TEST t设置Q_1_volume = v_risk_volume / rec.Q_VOLUME,Q_1_amount = v_risk_amount / rec.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;

2 个答案:

答案 0 :(得分:1)

  

v_sql:='更新PFPL_RISKPROFILE_RES_TEST t设置Q_'|| i_cnt ||'_volume = v_risk_volume / rec.Q_VOLUME,Q_'|| i_cnt ||'_ amount = v_risk_amount / rec.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

答案 1 :(得分:0)

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

第一个错误是使用EXECUTE IMMEDIATE,执行的sql不应该包含任何引用,我用绑定变量替换它就可以了。

第二个错误是当内部光标从1..32开始时,对于所有32次迭代,没有必要选择v_risk_volume和v_risk_amount的值,并且该过程在第一次迭代时崩溃,其中select ...进......没什么都没回来。

所以我更改了逻辑,将内部光标放在select ... into ...语句中的表上,现在它运行得很完美。我对这个解决方案非常满意,它使用最少的代码完成工作,并且因为处理的最大记录数是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;