1

これを確認して、エラーが発生した理由を教えていただけますか? どのように見えるべきですか?ここで何が問題なのかわかりません。関数でテーブルを作成する必要があり、同じ関数でこのテーブルにデータを挿入します。

create or replace
function new_tab ( pyt IN varchar2) return number
IS
a number;
b varchar2(20);
begin
a:= ROUND(dbms_random.value(1, 3));
b:='example';

-- This works perfect
execute immediate 'CREATE TABLE create_tmp_table'|| a  ||'(i VARCHAR2(50))';

-- Here`s the problem
execute immediate 'insert into create_tmp_table'||a|| 'values ('|| b ||')'; 

exception
when others then
dbms_output.put_line('ERROR-'||SQLERRM);

return 0;
end;

私の結果は次のとおりです。 ERROR-ORA-00926: missing VALUES keyword. Process exited.

間違いはどこですか?

4

2 に答える 2

1

動的挿入コマンドを作成しているので、そのまま作成する必要があります。したがって、varchar である値の一重引用符がありません。

execute immediate 
     'insert into create_tmp_table'||a|| ' values ('''|| b ||''');'; 
                                                    ^here     ^and here

このタイプのエラーに対する適切な提案は、デバッグを行うことです。あなたのケースでは、varchar2 変数を作成し、それに挿入してdbms_output.put_lineから、この変数に を使用できます。次に、データベースで直接テストできる sql コマンドを取得します。何かのようなもの:

declare
   sqlCommand varchar2(1000);
   --define a and b
begin
   --put some values in a and b
   sqlCommand := 'insert into create_tmp_table'||a|| ' values ('''|| b ||''');';
   dbms_output.put_line(sqlCommand);
end;

次に、動的コマンドの何が問題なのかがわかります。

于 2013-12-09T19:09:52.743 に答える