proc之建表添加数据报错解决

需求是:我想要创建一个存储过程,在这个存储过程里要完成两件事情,一要创建一个表,二要在这个表里添加数据;

create or replace procedure ctab as
i number;
begin
execute immediate 'create table dd as
select * from dept';
execute immediate 'alter table dd add constraint pk_dd primary key(deptno)';

for i in 901 .. 10000 loop
insert into dd values (i, 'TEST', 'BEIJING');
end loop;
commit;

end;

编译报错,提示表DD不存在。

编译器认为这个表DD是没有创建,所以报错。下面是修改过的存储过程。

----------------------

存储过程完善

create or replace procedure ctab(o_flag out varchar2) as
i number;
begin
select count(*) into i from user_tables where table_name = 'DD';
if i = 0 then
execute immediate 'create table dd as
select * from dept';
execute immediate 'alter table dd add constraint pk_dd primary key(deptno)';
execute immediate 'alter table dd modify deptno number(9)';
for j in 100 .. 10000 loop
execute immediate 'insert into dd values(:1,:2,:3)'
using j,'HR','BEIJING';
end loop;
commit;
end if;
o_flag := '0';
exception
when others then
o_flag := '1';
dbms_output.put_line('SQL_CODE:'||sqlcode||chr(10)||sqlerrm||chr(10)||
dbms_utility.format_error_stack());
rollback;
return;
end;

问题解决^_^

 

调用带返回参数的存储过程方法: 

var o varchar2(1);

exec ctab(:o);

 

posted @ 2018-05-26 23:14  王洁  阅读(221)  评论(0编辑  收藏  举报