一个、概要

在实际的软件开发项目。我们经常会遇到需要创造更多的相同类型的数据库表或存储过程时,。例如。假设按照尾号点表的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。请扫描下面二维码:

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

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

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

    创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...

  2. 5.oracle建表的时候同时创建主键,外键,注释,约束,索引

    5.oracle建表的时候同时创建主键,外键,注释,约束,索引 1 --主键 )); ) ,constraint aba_pr primary key(id,name1)); --外键 )); --复 ...

  3. PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写

    原文:PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入T ...

  4. oracle 建表时显示ORA-00984: 列在此处不允许

      oracle 建表时显示ORA-00984: 列在此处不允许 CreationTime--2018年7月19日16点10分 Author:Marydon 1.情景展示 使用plsql建表时,报错 ...

  5. oracle 建表时显示ORA-00904无效的标识符

      oracle 建表时显示ORA-00904无效的标识符 CreationTime--2018年7月19日16点03分 Author:Marydon 1.情景展示 使用plsql建表时,报错 字段展 ...

  6. oracle 建表 主键自增序列/////

    oracle 建表 主键自增序列 (2011-10-12 11:59:22) 转载▼ 标签: 杂谈 分类: oracle SQL> create table sms_activity(  2   ...

  7. Oracle建表提示SQL 错误: ORA-00904: : 标识符无效

    Oracle建表提示: 错误报告:SQL 错误: ORA-00904: : 标识符无效00904. 00000 -  "%s: invalid identifier"*Cause: ...

  8. Oracle数据库常用Sql语句大全

    一,数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSE ...

  9. Oracle,SQL Server 数据库较MySql数据库,Sql语句差异

    原文:Oracle,SQL Server 数据库较MySql数据库,Sql语句差异 Oracle,SQL Server 数据库较MySql数据库,Sql语句差异 1.关系型数据库 百度百科 关系数据库 ...

随机推荐

  1. Path相关评论的方法(一)

    以前的主要是关于Canvas的translate(平移) .scale(缩放) .rotate(旋转) .skew(错切).接下来几篇主要讲下android里的Path(封装了贝塞尔曲线)&  ...

  2. JQuery打印

    jquery.jqprint-0.3.js JQuery提供的局部打印功能: <input type="button" value="打印" onclic ...

  3. Android_declare-styleable_自己定义控件的属性

    1.简单实例 (1).在res/values文件下定义一个attrs.xml文件 <? xml version="1.0" encoding="utf-8" ...

  4. 凝视条件推断浏览器&lt;!--[if !IE]&gt;&lt;!--[if IE]&gt;&lt;!--[if lt IE 6]&gt;&lt;!--[if gte IE 6]&gt;

    <!--[if !IE]><!--> 除IE外可识别 <!--<![endif]--> <!--[if IE]> 所有的IE可识别 <![e ...

  5. unrecognized selector sent to instance 0x10b34e810

    一个错误: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLEr ...

  6. 检验身份证的正确性(Golang版本)

    // CheckID_card project main.go package main /* * ai -> a1 , a2, a3, a4, a5, a6... a17 (a18 是校验码) ...

  7. Zookeeper分享

    Zookeeper: 是一个分布式的,为分布式应用提供数据一致性服务的程序. Zookeeper是怎么来的? 分布式系统:是一个硬件或软件组件分布在不同的网络计算机上,彼此之间仅仅通过消息传递进行通信 ...

  8. Objective-C代码块语法(block)使用

    和其它变量本质上相似的代码块.所不同的是,数据存储代码块是主体的函数. 使用的代码块被,你可以像打电话一样等标准功能,传入参数的数量,并获得返回值. 插入符号(^)语法标记块.我们熟悉的参数按照规约定 ...

  9. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  10. 左右db_block_size了解和实验

    关于db_block_gets了解和实验 实验 一. 自己手动创建的小表 创建一个区大小为  40k  SYS@ORCL>show parameter db_block_size NAME   ...