ee_cust表中有emp_num,
从table_ee1表中取到emp_number=ee_cust.emp_num的数据插入table_ee表
存储过程如下:
CREATE OR REPLACE PROCEDURE ee_procedure
is vsql varchar2(100);
cursor ee_cust_cursor is select emp_num,begin_date from ee_cust;
emp_num ee_cust.emp_num%type;
begin_date ee_cust.begin_date%type;
begin
open ee_cust_cursor;
loop
fetch ee_cust_cursor INTO emp_num, begin_date;
EXIT WHEN ee_cust_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_num||'/'||begin_date);
vsql:=' Insert into table_ee (bill_month,emp_number) select bill_month,emp_number from table_ee1 where emp_number= '''||emp_num||'''';
execute immediate vsql;
commit;
end loop;
close ee_cust_cursor ;
end ee_procedure;
可是执行出错!!不知为何?经检查,table_ee1表和table_ee表和ee_cust表的emp_number均为 VARCHAR2(15);table_ee1表和table_ee表的字段结构一模一样,求好心人告知解决方法,谢谢!!
SQL> execute ee_procedure;
55487887/20060711begin ee_procedure; end;ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "ee_PROCEDURE", line 16
从table_ee1表中取到emp_number=ee_cust.emp_num的数据插入table_ee表
存储过程如下:
CREATE OR REPLACE PROCEDURE ee_procedure
is vsql varchar2(100);
cursor ee_cust_cursor is select emp_num,begin_date from ee_cust;
emp_num ee_cust.emp_num%type;
begin_date ee_cust.begin_date%type;
begin
open ee_cust_cursor;
loop
fetch ee_cust_cursor INTO emp_num, begin_date;
EXIT WHEN ee_cust_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_num||'/'||begin_date);
vsql:=' Insert into table_ee (bill_month,emp_number) select bill_month,emp_number from table_ee1 where emp_number= '''||emp_num||'''';
execute immediate vsql;
commit;
end loop;
close ee_cust_cursor ;
end ee_procedure;
可是执行出错!!不知为何?经检查,table_ee1表和table_ee表和ee_cust表的emp_number均为 VARCHAR2(15);table_ee1表和table_ee表的字段结构一模一样,求好心人告知解决方法,谢谢!!
SQL> execute ee_procedure;
55487887/20060711begin ee_procedure; end;ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "ee_PROCEDURE", line 16
DBMS_OUTPUT.PUT_LINE(emp_num||'/'||begin_date);
把上面这句删掉试下。
DBMS_OUTPUT.PUT_LINE(emp_num||'/'||begin_date);
这句只是把取得的变量显示出来,看看又没有取得变量啊,应该没有关系的。
哪位大哥再帮看下
CREATE OR REPLACE PROCEDURE ee_procedure
is cursor ee_cust_cursor is select emp_num,begin_date from ee_cust;
emp_num ee_cust.emp_num%type;
begin_date ee_cust.begin_date%type;
begin
open ee_cust_cursor;
loop
fetch ee_cust_cursor INTO emp_num, begin_date;
EXIT WHEN ee_cust_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_num||'/'||begin_date);
Insert into table_ee (bill_month,emp_number) select bill_month,emp_number from table_ee1 where emp_number= ''||emp_num||'';
end loop;
close ee_cust_cursor ;
end ee_procedure;
如果emp_num是数字类型使用上面的格式,是字符的话把''||emp_num||''换为emp_num就可以了
没有必要用动态执行语句
但是,有一个问题。
就是我必须要用动态的执行语句。因为我的表名是动态的一天一个表。我上面没有将这个列出来 vsql:=' Insert into table_ee (bill_month,emp_number) select bill_month,emp_number from table_ee1_'||begin_date||' where emp_number= '''||emp_num||'''';麻烦大哥再帮看看
http://www.databasejournal.com/features/oracle/article.php/2109681
table_ee1_20060711 ~table_ee1_20060731
这个语句是可以执行成功的,说明这个begin_date这个参数没问题,
再改成下面这段,也可成功执行
vsql:=' Insert into table_ee (emp_number) select emp_number from table_ee1_'||begin_date;这就说明了两个问题,是这个地方
where emp_number= '''||emp_num||''''有问题
还有Insert into table_ee (bill_month,emp_number)有问题,不能同时插两个?