指令碼:線上重定義,從普通表到分割槽表,redefinition_table.sh

raysuen發表於2018-03-09
使用方法:
    redefinition_table.sh -o 普通表 -i 分割槽表 -u 表所在的使用者名稱 -f 使用哪個方式重定義:[rowid|pk] -a 動作[redefine] -c oracle連線字串:ora_name/ora_pass@tnsname
    -c 如果連線本地資料庫可以不指定,預設為/ as sysdba
    -f 提供rowid或者pk兩個值,預設為pk
    -a 表示需要執行那個動作,重定義還是終止定義。預設:redefine


點選(此處)摺疊或開啟

  1. #!/bin/bash
  2. #by raysuen
  3. #version 2.0


  4. [ -e ~/.profile ] && . ~/.profile
  5. [ -e ~/.bash_profile ] && . ~/.bash_profile


  6. ###################################################
  7. #The function is check that the User exists in the database.
  8. ###################################################
  9. CheckUser(){
  10.     sqlplus -s /nolog<<-RAY
  11.         set termout off;
  12.         set echo off;
  13.         set feedback off;
  14.         set verify off;
  15.         set heading off;
  16.         conn $2
  17.         select username from dba_users where username='$1';
  18.     RAY
  19. }


  20. ###################################################
  21. #The function is check that the table exists in the database.
  22. ###################################################

  23. CheckTable(){
  24.     sqlplus -s /nolog<<-RAY
  25.         set termout off;
  26.         set echo off;
  27.         set feedback off;
  28.         set verify off;
  29.         set heading off;
  30.         conn $3
  31.         select table_name from dba_tables where owner='$1' and table_name='$2';
  32.     RAY
  33. }

  34. ###################################################
  35. #The function is check that can redefinition table.
  36. ###################################################
  37. CanRedef(){
  38.     sqlplus -s /nolog<<-RAY
  39.         conn $1
  40.         set termout off;
  41.         set echo off;
  42.         set verify off;
  43.         set heading off;
  44.         $2
  45.     RAY
  46. }

  47. ###################################################
  48. #The function that redefinition table
  49. ###################################################
  50. redefinitionTable(){
  51.     sqlplus -s /nolog<<-RAY
  52.         conn $4
  53.         set termout off;
  54.         set echo off;
  55.         set verify off;
  56.         set heading off;
  57.         alter session force parallel dml;
  58.         alter session force parallel query;
  59.         $5
  60.         DECLARE
  61.             num_errors PLS_INTEGER;
  62.         BEGIN
  63.               DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS('$1', '$2','$3',
  64.                DBMS_REDEFINITION.CONS_ORIG_PARAMS, TRUE, TRUE, TRUE, TRUE, num_errors);
  65.         END;
  66.         /
  67.         exec dbms_redefinition.sync_interim_table(uname => '$1',orig_table => '$2',int_table => '$3');
  68.         exec dbms_redefinition.finish_redef_table(uname => '$1',orig_table => '$2',int_table => '$3');
  69.     RAY
  70. }

  71. ###################################################
  72. #The function that abort table
  73. ###################################################
  74. AbortTable(){
  75.     sqlplus -s /nolog<<-RAY
  76.         conn $4
  77.         set termout off;
  78.         set echo off;
  79.         set verify off;
  80.         set heading off;
  81.         DBMS_REDEFINITION.ABORT_REDEF_TABLE($1,$2,$3)
  82.     RAY
  83. }


  84. ###################################################
  85. #Checkt to chennect oracle
  86. ###################################################
  87. CheckConnect(){
  88.     sqlplus /nolog<<-RAY
  89.         conn $1
  90.     RAY
  91. }



  92. ###################################################
  93. #The function for help
  94. ###################################################

  95. func_help(){
  96.     echo "Example:"
  97.     echo "    redefinition_table.sh -o origins_table -i interlayer_table -u user_name -f option_flag -a action -c ora_user/ora_passwd@tnsname"
  98.     echo "Value:"
  99.     echo "    -c default value [/ as sysdba]"
  100.     echo "    -f to use the way which redefine table."
  101.     echo "        value: rowid/pk,default value: PK."
  102.     echo "    -a specify a action which you want to do,default: redefine"
  103.     
  104. }



  105. ###################################################
  106. #Entrance of the script
  107. ###################################################

  108. ###################################################
  109. #get parameter
  110. ###################################################
  111. if [ $# -lt 1 ];then
  112.     echo "no parameters"
  113.     exit 99
  114. else
  115.     while (($#>=1))
  116.     do
  117.         #echo $#
  118.         if [ $1 == "-o" ];then #-o origins
  119.             shift
  120.             orig_table=`echo $1 | tr [a-z] [A-Z]`
  121.             shift
  122.         elif [ $1 == "-i" ];then #-i interlayer
  123.             shift
  124.             int_table=`echo $1 | tr [a-z] [A-Z]`
  125.             shift
  126.         elif [ $1 == "-u" ];then
  127.             shift
  128.             user_name=`echo $1 | tr [a-z] [A-Z]`
  129.             shift
  130.         elif [ $1 == "-c" ];then #conect string include ora_username/ora_pwd@tns_name
  131.             shift
  132.             oraConStr=`echo $1`
  133.             shift
  134.         elif [ $1 == "-f" ];then #option_flag default: dbms_redefinition.cons_use_pk. values: rowid/PK
  135.             shift
  136.             option_flag=`echo $1 | tr [a-z] [A-Z]`
  137.             shift
  138.         elif [ $1 == "-a" ];then #action, the value must be redifine/abort. default: redifine
  139.             shift
  140.             action=`echo $1 | tr [a-z] [A-Z]`
  141.             shift
  142.         elif [ $1 == "-h" ];then
  143.             func_help
  144.             exit 0
  145.         else
  146.             echo "Please enter right parameter!"
  147.             echo "-h,you can use the parameter to get help!"
  148.             exit 99
  149.         fi
  150.     done
  151. fi

  152. #echo $orig_table
  153. #echo $int_table
  154. #echo $user_name

  155. ###################################################
  156. #Check that the value of orig_table
  157. ###################################################
  158. if [ ! ${orig_table} ];then
  159.     echo "-o must be specified!"
  160.     exit 1
  161. fi
  162. ###################################################
  163. #Check that the value of int_table
  164. ###################################################
  165. if [ ! ${int_table} ];then
  166.     echo "-i must be specified!"
  167.     exit 2
  168. fi
  169. ###################################################
  170. #Check that the value of user_name
  171. ###################################################
  172. if [ ! ${user_name} ];then
  173.     echo "-u must be specified!"
  174.     exit 3
  175. fi

  176. ###################################################
  177. #Check whether is successful to connecting oracle
  178. ###################################################
  179. [ ! ${oraConStr} ]&& oraConStr=' / as sysdba'

  180. CheckConnect "${oraConStr}" | grep "Connected" > /dev/null 2>&1
  181. if [ $? -ne 0 ];then
  182.         echo "it is fail to connect oracle using input connection string!"
  183.         exit 4
  184. fi

  185. ###################################################
  186. #Check whether the input user exists in the database.
  187. ###################################################
  188. if [ ! `CheckUser ${user_name} ${oraConStr} | sed 's/\n//g'` ];then
  189.     echo "User:("${user_name}") dose not exists in database!"
  190.     exit 5s
  191. fi

  192. ###################################################
  193. #Check whether the input table exists in the database.
  194. ###################################################
  195. if [ ! `CheckTable ${user_name} ${orig_table} ${oraConStr} | sed 's/\n//g'` ];then
  196.     echo "Table:("${orig_table}") dose not exists in database!"
  197.     exit 6
  198. fi
  199. if [ ! `CheckTable ${user_name} ${int_table} ${oraConStr} | sed 's/\n//g'` ];then
  200.     echo "Table:("${int_table}") dose not exists in database!"
  201.     exit 7
  202. fi


  203. ###################################################
  204. #Check the value of action be specified
  205. ###################################################
  206. [ ! ${action} ] && action="REDEFINE"

  207. ###################################################
  208. #Check whether can redefinition table.
  209. ###################################################
  210. if [ ${action} == 'REDEFINE' ];then
  211.     if [ ! ${option_flag} ];then
  212.         option_flag="PK"
  213.         canRedStr="exec dbms_redefinition.can_redef_table(""'"$user_name"'"",""'"$orig_table"'"");"
  214.         startRedStr="exec DBMS_REDEFINITION.start_redef_table(uname => '"$user_name"',orig_table => '"$orig_table"',int_table => '"${int_table}"');"
  215.     elif [ ${option_flag} == "PK" ];then
  216.         canRedStr="exec dbms_redefinition.can_redef_table(""'"$user_name"'"",""'"$orig_table"'"");"
  217.         startRedStr="exec DBMS_REDEFINITION.start_redef_table(uname => '"$user_name"',orig_table => '"$orig_table"',int_table => '"${int_table}"');"
  218.     elif [ ${option_flag} == "ROWID" ];then
  219.         canRedStr="exec dbms_redefinition.can_redef_table(""'"$user_name"'"",""'"$orig_table"',dbms_redefinition.cons_use_rowid);"
  220.         startRedStr="exec DBMS_REDEFINITION.start_redef_table(uname => '"$user_name"',orig_table => '"$orig_table"',int_table => '"${int_table}"',options_flag => dbms_redefinition.cons_use_rowid);"
  221.     else
  222.         echo "you must specify value for -f,only rowid/pk."
  223.         exit 8
  224.     fi
  225.     
  226.     if [ `CanRedef ${oraConStr} "${canRedStr}" | grep "successfully" | wc -l` -eq 0 ];then
  227.         echo ${orig_table}" can not redefine using "${option_flag}
  228.         exit 9
  229.     fi
  230.     ###################################################
  231.     #begin to redefinit table
  232.     ###################################################
  233.     redefinitionTable ${user_name} ${orig_table} ${int_table} "${oraConStr}" "${startRedStr}"
  234. elif [ ${action} == 'ABORT' ];then
  235.     AbortTable ${user_name} ${orig_table} ${int_table} "${oraConStr}"
  236. else
  237.     echo "The value of action only use [redefine|abort]."
  238.     exit 10
  239. fi



  240. ###################################################
  241. #core code
  242. ###################################################
  243. #exec dbms_redefinition.can_redef_table('$1', '$2');
  244. #exec DBMS_REDEFINITION.start_redef_table(uname => '$1',orig_table => '$2',int_table => '$3');
  245. #DECLARE
  246. #    num_errors PLS_INTEGER;
  247. #BEGIN
  248. #    DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS('$1', '$2','$3',
  249. #    DBMS_REDEFINITION.CONS_ORIG_PARAMS, TRUE, TRUE, TRUE, TRUE, num_errors);
  250. #END;
  251. #/
  252. #exec dbms_redefinition.sync_interim_table(uname => '$1',orig_table => '$2',int_table => '$3');
  253. #exec dbms_redefinition.finish_redef_table(uname => '$1',orig_table => '$2',int_table => '$3');











來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28572479/viewspace-2151678/,如需轉載,請註明出處,否則將追究法律責任。

相關文章