zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    cqyz oj | 单峰排列
    cqyz oj/uva 548 | 二叉树
    cqyz oj | 树网的核 | 树的直径
    cqyz oj | 树上的询问 | 最近公共祖先
    cqyz oj | 循环逆序对 | 逆序对 | 树状数组
    cqyz oj | 潜水比赛 | 贪心
    YOLO v3 & Pascal VOC数据集
    太阳爆发分类
    PPT制作
    anaconda
  • 原文地址:https://www.cnblogs.com/jwangpip/p/9094721.html
Copyright © 2011-2022 走看看