基于ORACLE建表和循环回路来创建数据库存储过程SQL语句来实现

一个、概要

在实际的软件开发项目。我们经常会遇到需要创造更多的相同类型的数据库表或存储过程时,。例如。假设按照尾号点表的ID号,然后,你需要创建10用户信息表,的用户信息放在同一个表中。

对于类型同样的多个表,我们能够逐个建立,也能够採用循环的方法来建立。与之相相应的,能够用一个存储过程实现对全部表的操作。也能够循环建立存储过程,每一个存储过程实现对某个特定表的操作。

本文中,我们建立10个员工信息表。每一个表中包括员工工号(8位)和年龄字段,以工号的最后一位来分表。同一时候,我们建立存储过程实现对员工信息的插入。本文中的SQL语句基于ORACLE数据库实现。

二、一般的实现方式

在该实现方式中,我们逐个建立员工信息表,并在一个存储过程实现对全部表的操作。详细SQL语句例如以下:

建表语句:

-- tb_employeeinfo0
begin
    execute immediate ‘drop table tb_employeeinfo0 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo0
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo0 on tb_employeeinfo0(employeeno);

prompt ‘create table tb_employeeinfo0 ok‘;
commit;

-- tb_employeeinfo1
begin
    execute immediate ‘drop table tb_employeeinfo1 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo1
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo1 on tb_employeeinfo1(employeeno);

prompt ‘create table tb_employeeinfo1 ok‘;
commit;

-- tb_employeeinfo2
begin
    execute immediate ‘drop table tb_employeeinfo2 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo2
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo2 on tb_employeeinfo2(employeeno);

prompt ‘create table tb_employeeinfo2 ok‘;
commit;

-- tb_employeeinfo3
begin
    execute immediate ‘drop table tb_employeeinfo3 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo3
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo3 on tb_employeeinfo3(employeeno);

prompt ‘create table tb_employeeinfo3 ok‘;
commit;

-- tb_employeeinfo4
begin
    execute immediate ‘drop table tb_employeeinfo4 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo4
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo4 on tb_employeeinfo4(employeeno);

prompt ‘create table tb_employeeinfo4 ok‘;
commit;

-- tb_employeeinfo5
begin
    execute immediate ‘drop table tb_employeeinfo5 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo5
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo5 on tb_employeeinfo5(employeeno);

prompt ‘create table tb_employeeinfo5 ok‘;
commit;

-- tb_employeeinfo6
begin
    execute immediate ‘drop table tb_employeeinfo6 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo6
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo6 on tb_employeeinfo6(employeeno);

prompt ‘create table tb_employeeinfo6 ok‘;
commit;

-- tb_employeeinfo7
begin
    execute immediate ‘drop table tb_employeeinfo7 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo7
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo7 on tb_employeeinfo7(employeeno);

prompt ‘create table tb_employeeinfo7 ok‘;
commit;

-- tb_employeeinfo8
begin
    execute immediate ‘drop table tb_employeeinfo8 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo8
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo8 on tb_employeeinfo8(employeeno);

prompt ‘create table tb_employeeinfo8 ok‘;
commit;

-- tb_employeeinfo9
begin
    execute immediate ‘drop table tb_employeeinfo9 cascade constraints‘;
    exception when others then commit;
end;

/
create table tb_employeeinfo9
(
    employeeno      varchar2(10)  not null,         -- employee number
    employeeage     int           not null          -- employee age
);
create unique index idx1_tb_employeeinfo9 on tb_employeeinfo9(employeeno);

prompt ‘create table tb_employeeinfo9 ok‘;
commit;

存储过程创建语句:

create or replace procedure pr_insertdata
(
    v_employeeno   in   varchar2,
    v_employeeage  in   int
)
as
    v_employeecnt     int;
    v_tableindex      varchar2(2);

begin
    v_tableindex     := substr(v_employeeno, length(v_employeeno), 1);

    if v_tableindex = ‘0‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo0 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo0(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘1‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo1 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo1(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘2‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo2 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo2(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘3‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo3 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo3(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘4‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo4 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo4(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘5‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo5 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo5(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘6‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo6 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo6(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘7‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo7 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo7(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘8‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo8 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo8(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    elsif v_tableindex = ‘9‘ then
    begin
        select count(*) into v_employeecnt from tb_employeeinfo9 where employeeno = v_employeeno;
        if v_employeecnt > 0 then       -- the employeeno is already in DB
        begin
            return;
        end;
        else                            -- the employeeno is not in DB
        begin
            insert into tb_employeeinfo9(employeeno, employeeage) values(v_employeeno, v_employeeage);
        end;
        end if;
    end;
    end if;
    commit;

exception when others then
    begin
        rollback;
        return;
    end;
end;
/
prompt ‘create procedure pr_insertdata ok‘

三、循环创建的实现方式

在该实现方式中,我们採用循环的方法建立员工信息表及存储过程。详细SQL语句例如以下:

建表语句:

-- tb_employeeinfo0~9
begin
     declare i int;tmpcount int;tbname varchar2(50);strsql varchar2(1000);
     begin
         i:=0;
         while i<10 loop
         begin
             tbname := ‘tb_employeeinfo‘||to_char(i);
             i := i+1;

             select count(1) into tmpcount from user_tables where table_name = Upper(tbname);
             if tmpcount>0 then
             begin
                 execute immediate ‘drop table ‘||tbname;
             commit;
             end;
             end if;
             strsql := ‘create table ‘||tbname||
             ‘(
                  employeeno      varchar2(10)  not null,         -- employee number
                  employeeage     int           not null          -- employee age
              )‘;
             execute immediate strsql;
             strsql := ‘begin
                  execute immediate ‘‘drop index idx1_‘||tbname || ‘ ‘‘‘
                  || ‘;exception when others then null;
                  end;‘;
             execute immediate strsql;

             execute immediate ‘create unique index idx1_‘||tbname||‘ on ‘||tbname||‘(employeeno)‘;

         end;
         end loop;
     end;
end;
/

存储过程创建语句:

begin
    declare v_i int;v_procname varchar(50);v_employeeinfotbl varchar(50);strsql varchar(4000);
begin
    v_i := 0;
    while v_i < 10 loop
        v_procname        := ‘pr_insertdata‘||substr(to_char(v_i),1,1);
        v_employeeinfotbl := ‘tb_employeeinfo‘||substr(to_char(v_i),1,1);

        v_i := v_i + 1;
        strsql := ‘create or replace procedure ‘||v_procname||‘(
            v_employeeno   in   varchar2,
            v_employeeage  in   int
        )
        as
            v_employeecnt     int;

        begin
            select count(*) into v_employeecnt from ‘||v_employeeinfotbl||‘ where employeeno = v_employeeno;
            if v_employeecnt > 0 then       -- the employeeno is already in DB
            begin
                return;
            end;
            else                            -- the employeeno is not in DB
            begin
                insert into ‘||v_employeeinfotbl||‘(employeeno, employeeage) values(v_employeeno, v_employeeage);
            end;
            end if;
            commit;
        exception when others then
            begin
                rollback;
                return;
            end;
        end;‘;
        execute immediate strsql;
    end loop;
    end;
end;
/

四、总结

当同样类型的表的个数较多时(如有上百个)。显然用循环创建的实现方式能够节约大量的工作时间,提高工作效率。可是,在使用该方法的时候,要特别细致,尤其要注意单引號的使用,避免为了省事而引入代码逻辑问题。



本人微信公众号:zhouzxi。请扫描下面二维码:

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-09-28 23:09:22

基于ORACLE建表和循环回路来创建数据库存储过程SQL语句来实现的相关文章

SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束

创建数据库: 1 CREATE DATABASE Test --要创建的数据库名称 2 ON PRIMARY 3 ( 4 --数据库文件的具体描述 5 NAME='Test_data', --主数据文件的逻辑名称 6 FILENAME='E:\project\Test_data.mdf', --主数据文件的物理名称 7 SIZE=5MB, --主数据文件的初始大小 8 MAXSIZE=100MB, --主数据文件的增长的最大值 9 FILEGROWTH=15% --主数据文件的增长率 10 )

java 操作oracle 建表,更新记录

1.  建立表的类 import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; public class lx01{ public static void main(String[] args) throws SQLException, ClassNotFoundE

Oracle 建表常用数据类型的详解

创建表时,必须为表的各个列指定数据类型.如果实际的数据与该列的数据类型不相匹配,则数据库会拒绝保存.如为学生指定出生日期为"1980-13-31". 在Oracle中,常见的数据类型有: 字符串:字符串分为定长类型char和变长类型varchar2. 数字:整数 number(整数位),小数 number(总长度,小数位),只写number,表示无限制. 日期:date类型,可以保存年月日时分秒. 问题:Oracle中为什么字符串类型为varchar2,它与varchar有什么关系?

Oracle数据库常用Sql语句大全

一,数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, --) VALUES ( 值1, 值2, --); INSERT INTO 表名(字段名1, 字段名2, --)  SELECT (字段名1, 字段名2, --) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: 'GOOD DAY' 如果字段值里包含单引号' 需要进行字符串转换, 我们把它替换成两个单引号''. 字符串类型的字段值超过

数据库表设计的很灵活,是否做SQL语句也那么容易呢

由于项目需要,我们把一些不经常变的常数通过数据字段配置好,系统初始化的时候通过数据库字段去更新数据.下面就实例说明. 我有一张这样的表 ,你会发现meterkindid和measureid是代码,只有通过数据配置的数据字典才能解析出我们要的值,下面为数据字典表结构 ,这样设计就很灵活,FieldID为列名称,ID为上面表的值,value为解析值,也就是代码对应的名称,下面再发一张字典的数据图 MK001和MK002对应数据字典的水表跟电表,MS001和MS002对应数据字典的计量单位分别为吨还是

基于ORACLE数据库的循环建表及循环创建存储过程的SQL语句实现

一.概述 在实际的软件开发项目中,我们经常会遇到需要创建多个相同类型的数据库表或存储过程的时候.例如,如果按照身份证号码的尾号来分表,那么就需要创建10个用户信息表,尾号相同的用户信息放在同一个表中. 对于类型相同的多个表,我们可以逐个建立,也可以采用循环的方法来建立.与之相对应的,可以用一个存储过程实现对所有表的操作,也可以循环建立存储过程,每个存储过程实现对某个特定表的操作. 本文中,我们建立10个员工信息表,每个表中包含员工工号(8位)和年龄字段,以工号的最后一位来分表.同时,我们建立存储

Oracle建表插数据等等

Oracle的表的管理:表名和列的命名规则 . 必须以字母开头 . 长度不能超过30个字符 . 不能使用Oracle的保留字 . 只能使用如下字符 column_name-Z,column_name-z,0-9,$,#等 Oracle支持的数据类型 字符类:char 定长,最大2000个字节. 例子:char(10) 如果内容为'小杰'则前四个字节放'小杰',后添6个空格补全varchar2() 变长 最大4000个字符. 例子:varchar2(10) '小杰',Oracle分配四个字符.这样

Oracle建表,建主键,自增

--建表 create table Test( id number(4) not null primary key, name varchar2(25) ) --序列 create sequence Test_sequence increment by 1--每次增加几个 start with 1  --从1开始计数 minvalue 1 nomaxvalue    --不设置最大值 nocycle       --一直累加,不循环 nocache       --不建缓冲区 --触发器 cre

oracle 建表空间-&gt;创建用户并把表空间分配给用户-&gt;给用户授权-&gt;导库

首先注意:我参考网上使用的sysdba模式(normal)登陆的,其他的模式建不了用户(个人没有进行其他模式的表空间尝试,如有人尝试欢迎补充,感激不尽) 表空间相当于表的容器(一下所有的操作都适用于oracle10g): --建表空间 create tablespace SOA                        --soa是表空间的名字 datafile 'D:\ a.dbf' size 50m                     --  'D:\ a.dbf' 表空间的存储路径