我编写了一个pl sql脚本,用于将存储在数组中的值添加到临时表中。我已经使用立即执行在循环内执行插入查询。
我检查了单引号和逗号的数量,它们已关闭。仍然我收到错误代码ORA-00917:缺少逗号。
declare
type post_CODS IS VARRAY(34) OF VARCHAR2(20);
type locality_ID IS VARRAY(34) OF NUMBER(9);
pc post_CODS;
Y varchar2(1);
lid locality_ID;
total integer;
begin
Y := 'Y';
lid := locality_ID(2380,3785,8710,17895,20345,24630,26814,28525,29130,31025,31265,32445,36940,40590,54290,83775,83780,83785,83790,83795,88483,93480,94560,96670,1000524,1000628,1000738,1000857,1000988,1001103,1001466,1001575,1001707,1001744);
pc := post_CODS(2822,2739,2822,2372,2817,4314,4300,4726,4300,4884,4314,4314,4300,5719,3026,4314,4314,4314,4314,4314,873,2582,2817,4314,5723,5440,2083,2575,873,2372,5440,5440,5715,5440);
total := lid.count;
FOR i in 1 .. total LOOP
execute immediate 'insert into tmp_ref_lc_pc_cods values('||lid(i)||','||pc(i)||','||Y||',89987,'||sysdate||')';
END LOOP;
END;
这是我得到的错误
ORA-00917: missing comma
ORA-06512: at line 14
00917. 00000 - "missing comma"
我正在使用Oracle计算机。
答案 0 :(得分:1)
尝试立即执行时的一个好技巧是确保所创建的语句的格式正确,打印您编写的代码后,您会发现Y不在撇号之内
insert into tmp_ref_lc_pc_cods values(2380,2822,Y,89987,23-APR-19)
因此,在动态创建代码时,请确保所创建的所有字符串都具有正确的撇号。
dbms_output.put_line('insert into tmp_ref_lc_pc_cods values('||lid(i)||','||pc(i)||','''||Y||''',89987,'||sysdate||')');
答案 1 :(得分:0)
在insert
中包装q'[]'
语句,然后尝试
execute immediate q'[insert into tmp_ref_lc_pc_cods values('||lid(i)||','||pc(i)||','||Y||',89987,'||sysdate||')]';
更新
建议使用 bind variable
。您可以尝试以下方法。它正在为我工作并插入数据。
declare
type post_CODS IS VARRAY(34) OF NUMBER(20);
type locality_ID IS VARRAY(34) OF NUMBER(9);
pc post_CODS;
Y varchar2(1);
lid locality_ID;
total integer;
begin
Y := 'Y';
lid := locality_ID(2380,3785,8710,17895,20345,24630,26814,28525,29130,31025,31265,32445,36940,40590,54290,83775,83780,83785,83790,83795,88483,93480,94560,96670,1000524,1000628,1000738,1000857,1000988,1001103,1001466,1001575,1001707,1001744);
pc := post_CODS(2822,2739,2822,2372,2817,4314,4300,4726,4300,4884,4314,4314,4300,5719,3026,4314,4314,4314,4314,4314,873,2582,2817,4314,5723,5440,2083,2575,873,2372,5440,5440,5715,5440);
total := lid.count;
FOR i in 1 .. total LOOP
execute immediate q'[insert into tmp_ref_lc_pc_cods values(:var1,:var2,'Y',89987,sysdate)]' using lid(i),pc(i);
END LOOP;
END;