温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

获取sql完整脚本,get_fulltext.sh

发布时间:2020-08-07 07:37:16 来源:ITPUB博客 阅读:197 作者:raysuen 栏目:关系型数据库

点击(此处)折叠或打开

  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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI