execute immediate
の動的SQL文を作成するときに、文字列連結の代わりにバインド変数を使用したいとします。私はa
、b
とret
のためのバインド変数を使用することができますが、私はf
ためにバインドしようとしたとき、私はORA-06502: PL/SQL: numeric or value error: character to number conversion error
を取得する以下の例では実行即時ステートメントでバインド変数を使用できないのはなぜですか?
。どうして私はバインドすることができますf
?
私は11.2.0.1.0を使用しています。あなたはvarchar2
としてf
を宣言しているため
create or replace function so4fun (
a in number,
b in number,
f in varchar2
) return number as
decl constant varchar2(32767) :=
'declare a constant number := :a; b constant number := :b;';
stmt varchar2(32676);
ret number;
begin
/* This one works: */
stmt := decl || ' begin :result := ' || f || '; end;';
execute immediate stmt using in a, in b, out ret;
/* But why this one doesn't ?
stmt := decl || ' begin :result := :f; end;';
execute immediate stmt using in a, in b, out ret, in f;
This doesn't work either:
stmt := decl || ' tmp number; begin tmp := :f; :result := tmp; end;';
execute immediate stmt using in a, in b, in f, out ret;
Both are giving me ORA-06502: PL/SQL: numeric or value error: character to
number conversion error */
return ret;
end;
/
show errors
/* expected result when a = 1, b = 2 is 1.5 */
select so4fun(1, 2, '(a + b)/b') from dual;
drop function so4fun;
私は彼の答えをより包括的かつコンパクトに見ているので、私はCodoの答えを選択しました。しかし、私もRay Toalの答えが役に立つと分かりました。私は彼の答えから自分の問題への答えも結論づけることができました。私はそれらの両方に多くのupvotesを願っています! – user272735