前言:最早项目发版或迭代更新的时候,将项目所需的文件及脚本等资料打包给实施人员后,会出现一个问题:脚本中创建表或索引时如果表或索引已存在会报错,从而影响后续脚本的执行;
1、原脚本比如
//建表
create table tusertest(
id number(20,0) not null,
name varchar2(128) not null,
primary key(id)
);
//建索引
create index ACT_NAME on tusertest(name);
2、需要将1中单次脚本改为可重复执行的脚本
//建表
declare v_count int;
begin
select count(*) into v_count from user_tables where table_name=UPPER('tusertest');
if v_count = 0 then execute immediate
'create table tusertest(
id number(20,0) not null,
name varchar2(128) not null,
primary key(id)
)';
end if;
end;
/
//建索引
declare v_count int;
begin
select count(*) into v_count from user_ind_columns where index_name=UPPER('ACT_NAME');
if v_count = 0 then execute immediate
'create index ACT_NAME on tusertest(name)';
end if;
end;
/
这样的话脚本不论之前有没有打过,后续都可以重复执行,不会报错影响其他脚本正常运行
3、测试语句是否正确时,我在数据库里面创建了一个单独用户的用来测试这些
create user cmf identified by 1; //创建用户cmf密码为1
grant connect,resource to cmf; //给用户cmf授权连接和资源创建的权限