【发布时间】:2017-03-17 08:13:55
【问题描述】:
我试图使用动态 PL/SQL 为多个参数生成乘法算法。我同意有多种方法可以做到这一点,但这个乘法问题是我需要做的其他事情的 PoC。
所以这里是代码
declare
var1 number := 1;
var2 number := 2;
output number := 1;
varname varchar2(10);
begin
for counter in 1..2
loop
execute immediate q'[select var]'||counter||' * :val1 from dual' into output using output;
end loop;
dbms_output.put_line(output);
end;
所以我想做的是在运行时生成参数名称,然后评估该参数名称。
这可行吗?内心深处我不这么认为……但如果这不起作用,我希望能避免巨大的痛苦。 :)。顺便说一句,我在 Oracle 11GR2 上。
这是我运行代码时发生的情况。
Error starting at line : 1 in command -
declare
var1 number := 1;
var2 number := 2;
output number := 1;
varname varchar2(10);
begin
for counter in 1..2
loop
execute immediate q'[select var]'||counter||' * :val1 from dual' into output using output;
end loop;
dbms_output.put_line(output);
end;
Error report -
ORA-00904: "VAR1": invalid identifier
ORA-06512: at line 9
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Elapsed: 00:00:00.082
感谢和问候。
【问题讨论】:
-
运行代码时会发生什么?
-
@Alfabravo:它达到了我的预期......有没有其他方法可以做到这一点......
-
Var1是这个 pl/sql 块变量的局部变量。动态 SQL 不会看到它。对于它来说,它是一个它一无所知的标识符。在静态 SQL 中,所有 pl/sql 变量都变成了绑定变量。你想达到什么目的? -
然后定义集合。 Something like this
-
编辑您的问题并包含所有相关信息,而不是在评论部分逐条给出。
标签: oracle plsql dynamic-sql