获取sql完整脚本,get_fulltext.sh


点击(此处)折叠或打开

  1. #!/bin/bash
  2. # by ray
  3. # 2017-08-31
  4. #v0.1

  5. ##get sqltext from dbs_hist_sqltext,sqltext will be save current directory and file name will be sql_id.txt
  6. FromHist(){
  7. sqlplus -s /nolog <<-RAY
  8.     conn / as sysdba
  9.     set linesize 300
  10.     set serveroutput on
  11.     set feedback off
  12.     spool ./$1.txt
  13.     declare
  14.     l_buffer varchar2(3000);
  15.     l_amount binary_integer :=3000;
  16.     l_pos int :=1;
  17.     l_clob_length int;
  18.     sqlid varchar2(100) := '$1';
  19.     begin
  20.     select DBMS_LOB.getlength(sql_text) into l_clob_length from dba_hist_sqltext where sql_id=sqlid;
  21.     while l_pos<l_clob_length loop
  22.     select DBMS_LOB.SUBSTR(sql_text,l_amount,l_pos) into l_buffer from dba_hist_sqltext where sql_id=sqlid;
  23.     dbms_output.put(l_buffer);
  24.     l_pos:=l_pos+l_amount;
  25.     end loop;
  26.     dbms_output.put_line(' ');
  27.     end;
  28.     /
  29.     spool off
  30.     exit
  31. RAY
  32. }


  33. ##get sqltext from V$sqlarea,sqltext will be save current directory and file name will be sql_id.txt
  34. FromMomery(){
  35. sqlplus -s /nolog <<-RAY
  36.     conn / as sysdba
  37.     set linesize 300
  38.     set serveroutput on
  39.     set feedback off
  40.     spool ./$1.txt
  41.     declare
  42.     l_buffer varchar2(3000);
  43.     l_amount binary_integer :=3000;
  44.     l_pos int :=1;
  45.     l_clob_length int;
  46.     sqlid varchar2(100) := '$1';
  47.     begin
  48.     select DBMS_LOB.getlength(sql_fulltext) into l_clob_length from v\$sqlarea where sql_id=sqlid;
  49.     while l_pos<l_clob_length loop
  50.     select DBMS_LOB.SUBSTR(sql_fulltext,l_amount,l_pos) into l_buffer from v\$sqlarea where sql_id=sqlid;
  51.     dbms_output.put(l_buffer);
  52.     l_pos:=l_pos+l_amount;
  53.     end loop;
  54.     dbms_output.put_line(' ');
  55.     end;
  56.     /
  57.     spool off
  58.     exit
  59. RAY
  60. }

  61. func_help(){
  62.     echo "--from specifying how to get sqltext,memery or hist can be used.default momery!!"
  63.     echo "--sqlid specify a sql_id"
  64.     echo "for example:"
  65.     echo "get_fulltext.sh --from=momery --sqlid=*********"
  66. }

  67. #get parameter
  68. argvs=($@)
  69. for i in ${argvs[@]}
  70. do
  71.         case `echo $i | awk -F '=' '{print $1}' | awk -F '--' '{print $2}'| tr [a-z] [A-Z]` in
  72.         FROM)
  73.             fm=`echo $i | awk -F '=' '{print $2}' | tr [a-z] [A-Z]`
  74.         ;;
  75.         SQLID)
  76.             sqlid=`echo $i | awk -F '=' '{print $2}' `
  77.         ;;
  78.         HELP)
  79.             func_help
  80.             exit 1
  81.         esac
  82. done

  83. if [ ! ${fm} ]; then
  84.     fm='MOMERY'
  85. fi
  86. if [ ! ${sqlid} ]; then
  87.     echo "The sql_id must be specified!!"
  88.     exit 1
  89. fi

  90. ##exec script
  91. if [ ${fm} == "HIST" ];then
  92.     FromHist "${sqlid}"
  93. elif [ ${fm} == "MOMERY" ];then
  94.     FromMomery "${sqlid}"
  95. else
  96.     echo "The type of parameter only are HIST or MOMERY!!"
  97. fi

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28572479/viewspace-2152081/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28572479/viewspace-2152081/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值