执行多sql文件的批处理(oracle)

本文介绍了一个用于数据库升级的批处理脚本,该脚本通过调用SQLPlus执行多个SQL文件来实现数据库结构更新及数据导入。同时提供了在SQL文件中创建表字段、序列和触发器的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

              项目中有多个数据库多处改变,另外有大量数据导入,为了方便升级从网上查找总结的批处理文件(此文件调用本地sqlplus执行)。

              此处只是简单的使用方法,研究不深只为了自己学习记录。


 

@echo ***************************************
@echo 数据库升级
@echo ***************************************

setlocal EnableDelayedExpansion
title 数据库升级

set usr=swater
set pwd=swater
set tns=orcl
set result=dbinfo.txt
date /T > %result%
time /T >> %result%

@echo. >> %result%

tnsping %tns% > nul

if errorlevel 1 (echo %tns% tnsping failing. && echo Error:tnsping failing. >> %result%) else (
     sqlplus -s %usr%/%pwd%@%tns% @"%~dp0define.sql"  >> %result%
     sqlplus -s %usr%/%pwd%@%tns% @"%~dp0admin.sql"  >> %result%
     sqlplus -s %usr%/%pwd%@%tns% @"%~dp0image.sql"  >> %result%
     sqlplus -s %usr%/%pwd%@%tns% @"%~dp0plan.sql"  >> %result%
     sqlplus -s %usr%/%pwd%@%tns% @"%~dp0hv.sql"  >> %result%
     echo. >> %result%
)

if exist sqlnet.log del sqlnet.log

@echo ***************************************
@echo 数据库导入数据执行完成
@echo ***************************************

pause 

notepad %result%

exit
 

 

 

set usr=swater //客户端访问用户名
set pwd=swater  //密码
set tns=orcl  //访问数据库的名称(tns,此处在product\11.2.0\client_1\network\admin 中tnsnames.ora中找)
set result=dbinfo.txt //输出文档的命名
tnsping %tns% > nul //测试tns是否ping通
%~dp0 //同目录文件夹得路径
notepad %result% //调出记录


 以下为sql的注意事项:

1.bat文件调用的sql文件中如果有存储过程一定要加 /

2.插入多条数据时commit ,然后在每个sql文件的最后写quit,这样才会结束弹出notepad。

为表增加字段 
declare  cnt number; 
 begin 
   ---查询要创建的表列是否存在
   SELECT COUNT(*) into cnt FROM cols WHERE table_name=UPPER('ST_BRIDGE') AND column_name=UPPER('VERSION');
   
   if cnt>0 then
      dbms_output.put_line('字段已经存在不能添加');
   else
       --dbms_output.put_line('字段不存在');
    execute immediate 'alter table ST_BRIDGE add VERSION number';
  end if;
    
end;
/
判断自增序列是否被创建
declare sequecesId number;
 begin
select COUNT(*) into sequecesId from user_sequences where sequence_name='S_ST_BRIDGE_ID';
  if sequecesId>0 then
    dbms_output.put_line('序列名称已经创建');
    execute immediate 'drop sequence S_ST_BRIDGE_ID';
    
    else
    dbms_output.put_line('序列名称不存在');
    execute immediate 'create sequence S_ST_BRIDGE_ID
    minvalue 1
    maxvalue 999999999
    start with 1
    increment by 1
    cache 20';
    --创建自增序列
  
    end if;
 end;
/
创建触发器
 declare triggerid number;
 begin
 select COUNT(*) into triggerid  from user_triggers where table_name='ST_BRIDGE';

    if triggerid>0 then
    dbms_output.put_line('已经存在');
    execute immediate 'drop TRIGGER Trig_ST_BRIDGE_ID';
    
    else
      --dbms_output.put_line(sequecesId);
     execute immediate 'CREATE OR REPLACE TRIGGER Trig_ST_BRIDGE_ID
    
     BEFORE INSERT ON ST_BRIDGE FOR EACH ROW
     DECLARE
     next_id NUMBER
     SELECT S_ST_BRIDGE_ID.NEXTVAL INTO next_id FROM dual
     :new.id:=next_id';
    end if;
 end;
/




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值