shell动态脚本和pl/sql动态脚本的比较

简介: 最近项目有一个需求,需要在多个数据库的schema上跑一些脚本。希望dba能够提供一个脚本,能够根据需求在环境中执行指定的脚本。 乍一听,没什么技术难点,为了更明白的说明问题,我举个例子。

最近项目有一个需求,需要在多个数据库的schema上跑一些脚本。希望dba能够提供一个脚本,能够根据需求在环境中执行指定的脚本。
乍一听,没什么技术难点,为了更明白的说明问题,我举个例子。
有4个DB Instance: DB1,DB2,DB3,DB4
有6个DB Schemas, 5个table,分布如下: 
                         db schemas        tables
                          user1@DB1        table1, table2, table3, table4,table5
                          user2@DB2        table1, table2, table3, table4,table5
                          user3@DB3        table1, table3
                          user4@DB4        table1, table4
                          user5@DB1        table1, table5
                          user6@DB2        table2, table3, table4
实现的目标如下,对于同时含有table1--5的db schema才需要执行指定的脚本,脚本内容都是些dml操作。
目前的情况只能够得到db schema的列表,对于里面是否还有5个表,还没有细粒度的管理。
脚本需要从db schema的列表中筛选出符合的 db schema,然后执行脚本内容。


################# pl/sql执行情况 #############################

#!/bin/ksh
export ScriptName=`basename $0`
export ScriptDir=`dirname $0`
echo $ScriptName
echo $ScriptDir
rep_conn=$1
Env_Code=$2
sqlplus -s ${rep_conn} set serveroutput on
set feedback off
spool app_change_tmp.log
declare
conn_str   varchar2(100);
target_conn varchar2(200);
tmp_cnts number;
cursor cur_conn_strs is
select distinct ''||username||'/'||password||'@'||db_instance||''   conn_str  from xxxx   --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
begin
for cur_conn_str in cur_conn_strs  loop
dbms_output.put_line('conn '||cur_conn_str.conn_str);
dbms_output.put_line('set serveroutput on');
dbms_output.put_line('set feedback on');
dbms_output.put_line('set echo on');
dbms_output.put_line('declare');
dbms_output.put_line('tmp_cnt number;');
dbms_output.put_line('begin');
dbms_output.put_line('select count(*) into tmp_cnt from user_synonyms where synonym_name');
dbms_output.put_line(' in('||chr(39)||'T1'||chr(39)||','||chr(39)||'T2'||chr(39)||','||chr(39)||'T3'||chr(39)||','||chr(39)||'T4'||chr(39)||','||chr(39)||'T5'||chr(39)||');');
dbms_output.put_line('dbms_output.put_line(tmp_cnt);');
dbms_output.put_line('if(tmp_cnt>=5) then ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('@script/script1.ps ');
dbms_output.put_line('@script/script2.ps ');
dbms_output.put_line('@script/script3.ps ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('end if;');
dbms_output.put_line('end;');
dbms_output.put_line('/');
dbms_output.put_line(tmp_cnts);
end loop;
end;
/
spool off;
@app_change_tmp.log
EOS


如上的Pl/sql生成的动态pl/sql如下, 先判断是否还有T1--T5,如果条数符合,就执行脚本内容,但是有个限制就是执行脚本的时候如果脚本中有“set linesize... set define off之类的设置的话,脚本是运行不了的,对于ddl的执行也有一些限制。

################# 生成的动态 pl/sql 如下 #############################

conn user1/user1@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');
end if;
end;
/


conn user2/user2@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');

end if;
end;
/

################# pl/sql执行情况 #############################


############## shell 脚本实现动态shell ################################

echo 'app CHANGE START....'
cat $ScriptDir/script1.ps > $ScriptDir/app_all.ps
cat $ScriptDir/script2.ps >> $ScriptDir/app_all.ps
cat $ScriptDir/script3.ps>> $ScriptDir/app_all.ps
echo `sqlplus -s  ${rep_conn} set feedback off  pages 0
select distinct ''||username||'/'||password||'@'||db_instance||''   conn_str  from xxxxxx   --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
EOF`|awk  '{for (i=1;i print  "set feedback off pages 0";
print "select (case when (select count(*) from user_synonyms where synonym_name in ('\''T5'\'','\''T1'\'','\''T2'\'','\''T3'\'','\''T4'\''))>=5 then  '\''Y @app_all.ps'\'' else '\''N no_need_to_run_app_script'\'' end) from dual; ";print "EOS` " $i}}'> $ScriptDir/dynamic_tmp.ksh
ksh $ScriptDir/dynamic_tmp.ksh |awk '{ if( $1 =="Y" ){ print "sqlplus -s " $3 " $ScriptDir/app_change_tmp.ksh
ksh $ScriptDir/app_change_tmp.ksh
rm $ScriptDir/dynamic_tmp.ksh
echo 'app CHANGE ENDED....'
rm $ScriptDir/app_change_tmp.ksh


#################生成的动态shell脚本1内容如下#################

echo `sqlplus -s user1/user1@DB1  set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then  'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS`  user1/user1@DB1

echo `sqlplus -s  user2/user2@DB2  set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then  'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS` user2/user2@DB2

#################执行动态shell脚本1后生成的脚本2内容如下#################

sqlplus -s user1/user1@DB1 @adj_all.ps
EOS
sqlplus -s user2/user2@DB2 @adj_all.ps
EOS

############## shell 脚本实现动态shell ################################

目录
相关文章
|
6天前
|
SQL 存储 Oracle
Oracle的PL/SQL定义变量和常量:数据的稳定与灵动
【4月更文挑战第19天】在Oracle PL/SQL中,变量和常量扮演着数据存储的关键角色。变量是可变的“魔术盒”,用于存储程序运行时的动态数据,通过`DECLARE`定义,可在循环和条件判断中体现其灵活性。常量则是不可变的“固定牌”,一旦设定值便保持不变,用`CONSTANT`声明,提供程序稳定性和易维护性。通过 `%TYPE`、`NOT NULL`等特性,可以更高效地管理和控制变量与常量,提升代码质量。善用两者,能优化PL/SQL程序的结构和性能。
|
1月前
|
SQL Perl
PL/SQL经典练习
PL/SQL经典练习
13 0
|
1月前
|
SQL Perl
PL/SQL编程基本概念
PL/SQL编程基本概念
13 0
|
1月前
|
SQL Perl
PL/SQL Developer 注册机+汉化包+用户指南
PL/SQL Developer 注册机+汉化包+用户指南
16 0
|
6天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL游标属性:数据的“导航仪”与“仪表盘”
【4月更文挑战第19天】Oracle PL/SQL游标属性如同车辆的导航仪和仪表盘,提供丰富信息和控制。 `%FOUND`和`%NOTFOUND`指示数据读取状态,`%ROWCOUNT`记录处理行数,`%ISOPEN`显示游标状态。还有`%BULK_ROWCOUNT`和`%BULK_EXCEPTIONS`增强处理灵活性。通过实例展示了如何在数据处理中利用这些属性监控和控制流程,提高效率和准确性。掌握游标属性是提升数据处理能力的关键。
|
6天前
|
SQL Oracle 安全
Oracle的PL/SQL循环语句:数据的“旋转木马”与“无限之旅”
【4月更文挑战第19天】Oracle PL/SQL中的循环语句(LOOP、EXIT WHEN、FOR、WHILE)是处理数据的关键工具,用于批量操作、报表生成和复杂业务逻辑。LOOP提供无限循环,可通过EXIT WHEN设定退出条件;FOR循环适用于固定次数迭代,WHILE循环基于条件判断执行。有效使用循环能提高效率,但需注意避免无限循环和优化大数据处理性能。掌握循环语句,将使数据处理更加高效和便捷。
|
6天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL条件控制:数据的“红绿灯”与“分岔路”
【4月更文挑战第19天】在Oracle PL/SQL中,IF语句与CASE语句扮演着数据流程控制的关键角色。IF语句如红绿灯,依据条件决定程序执行路径;ELSE和ELSIF提供多分支逻辑。CASE语句则是分岔路,按表达式值选择执行路径。这些条件控制语句在数据验证、错误处理和业务逻辑中不可或缺,通过巧妙运用能实现高效程序逻辑,保障数据正确流转,支持企业业务发展。理解并熟练掌握这些语句的使用是成为合格数据管理员的重要一环。
|
6天前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL表达式:数据的魔法公式
【4月更文挑战第19天】探索Oracle PL/SQL表达式,体验数据的魔法公式。表达式结合常量、变量、运算符和函数,用于数据运算与转换。算术运算符处理数值计算,比较运算符执行数据比较,内置函数如TO_CHAR、ROUND和SUBSTR提供多样化操作。条件表达式如CASE和NULLIF实现灵活逻辑判断。广泛应用于SQL查询和PL/SQL程序,助你驾驭数据,揭示其背后的规律与秘密,成为数据魔法师。
|
1月前
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
18 0
|
1月前
|
SQL Java 关系型数据库
MyBatis中的9种常用动态sql标签精妙用法
MyBatis中的9种常用动态sql标签精妙用法
61 0

热门文章

最新文章