首页 > 数据库技术 > 详细

pl/sql declare loop if

时间:2016-12-02 03:07:25      阅读:189      评论:0      收藏:0      [点我收藏+]
-- 1.判断表是否存在,如果存在则drop表
-- 2.创建表
-- 3.插入1W条数据
-- 4.每1K条commit一次
declare
    v_table varchar2(222):=STUDENT;   --表名
    v_table_exists number:=0;           --如果大于0,则表存在
    v_sql_create varchar2(2222);        --create table sql
    v_number number:=500000;              --插入的数据
    v_id number:=0;                     --id字段
    v_age number:=100000;               --age字段
    v_i number:=0;                      --记录插入的条数
    v_start_time varchar2(22);          --执行命令开始时间
    v_end_time varchar2(22);            --执行命令结束时间
    v_exec_time varchar(22);

begin
    --判断表是否存在,如果存在最删除
    select to_char(sysdate,yyyy-mm-dd hh24:mi:ss) into v_start_time from dual;
    select count(1) into v_table_exists from tab where tname=upper(student);
    if v_table_exists > 0
    then
        execute immediate drop table ||v_table|| purge;
        dbms_output.put_line(drop table ||v_table|| sucessfuly);
    else
        dbms_output.put_line(table ||v_table||  is not exists);
    end if;
    --create table student
    v_sql_create:=create table ||v_table|| (id number,age number);
    execute immediate v_sql_create;
    commit;
    -- insert data to student
    loop
        exit when v_number<=0;
        v_number:=v_number-1;
        insert into student values(v_id,v_age);
        v_id:=v_id+1;
        v_age:=v_age+1;
        v_i:=v_i+1;
        --commit / 10000 line data
        if mod(v_i,10000)=0
        then
            select to_char(sysdate,yyyy-mm-dd hh24:mi:ss) into v_exec_time from dual;
            dbms_output.put_line(v_i|| ||v_exec_time);
            commit;
        end if;
    end loop;
    select to_char(sysdate,yyyy-mm-dd hh24:mi:ss) into v_end_time from dual;
    dbms_output.put_line(execute sucess ||v_start_time|| -> ||v_end_time);
end;
/

 

pl/sql declare loop if

原文:http://www.cnblogs.com/chenzechao/p/6124188.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!