Linux/Unix 下除了调用SQL之外,调用PL/SQL也是DBA经常碰到的情形,下面主要通过一些示例给出如何在shell下面来调用pl/sql。
Linux/Unix 下除了调用SQL之外,调用PL/SQL也是DBA经常碰到的情形,下面主要通过一些示例给出如何在shell下面来调用pl/sql。
其它相关的参考:
Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell sql 之间传递变量
1、将pl/sql代码逐行输入到临时文件
robin@SZDB:~/dba_scripts/custom/bin> more shell_call_plsql.sh
#/bin/bash
# --------------------------------------------
# An example of calling plsql in Shell
# Usage:
# ./shell_call_plsql.sh $Oracle_SID
# Author: Robinson
# --------------------------------------------
#
# ---------------------------------
# Define variable and check SID
# ---------------------------------
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
if test $# -lt 1
then
echo You must pass a SID
exit
fi
ORACLE_SID=$1; export ORACLE_SID
# ---------------------------------
# Prepare plsql script
# ---------------------------------
echo "set serveroutput on size 1000000" > /tmp/plsql_scr.sql
echo "set feed off" >> /tmp/plsql_scr.sql
echo "declare" >> /tmp/plsql_scr.sql
echo "cursor c1 (param1 varchar2) is" >> /tmp/plsql_scr.sql
echo "select decode(substr(value, 1, 1), '?', param1 || substr(value, 2), value) dd" >> /tmp/plsql_scr.sql
echo "from v$parameter where name = 'background_dump_dest';" >> /tmp/plsql_scr.sql
echo "v_value v$parameter.value%type;" >> /tmp/plsql_scr.sql
echo "begin open c1 ('$ORACLE_HOME'); fetch c1 into v_value; close c1;" >> /tmp/plsql_scr.sql
echo "dbms_output.put_line(v_value);" >> /tmp/plsql_scr.sql
echo "end;" >> /tmp/plsql_scr.sql
echo "/" >> /tmp/plsql_scr.sql
# --------------------------------
# Execute plsql script
# --------------------------------
if [ -s /tmp/plsql_scr.sql ]; then
echo -e "Running SQL script to find out bdump directory... n"
$ORACLE_HOME/bin/sqlplus -s "/ as sysdba" > /tmp/plsql_scr_result.log @/tmp/plsql_scr.sql
EOF
fi
echo " Check the reslut "
echo "------------------------"
cat /tmp/plsql_scr_result.log
exit
#上面的代码是查询指定Oracle SID 的dump路径。
#通过逐行逐行的方式将代码添加到文件以形成pl/sql代码。
#需要注意转义字符的使用,对于parameter 的$符号,我们进行了转义。
robin@SZDB:~/dba_scripts/custom/bin> ./shell_call_plsql.sh CNBO1
Running SQL script to find out bdump directory...
Check the reslut
------------------------
/u02/database/CNBO1/bdump
2、一次性输入pl/sql代码到临时文件
robin@SZDB:~/dba_scripts/custom/bin> more shell_call_plsql_2.sh
#/bin/bash
# --------------------------------------------
# An example of calling plsql in Shell
# Usage:
# ./shell_call_plsql_2.sh $ORACLE_SID
# Author: Robinson
# --------------------------------------------
#
# ---------------------------------
# Define variable and check SID
# ---------------------------------
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
if test $# -lt 1
then
echo You must pass a SID
exit
fi
ORACLE_SID=$1; export ORACLE_SID
# ---------------------------------
# Prepare plsql script
# ---------------------------------
echo "
将服务器输出设置为大小 1000000
设置 feed 关闭
声明
光标 c1 (param1 varchar2) 是
选择解码(substr(value, 1, 1),'? ' , param1 || substr(value, 2), value) dd
from v$parameter where name = 'background_dump_dest';
v_value v$parameter.value%type;
begin
open c1 ('/users/oracle/OraHome10g');
获取 c1 到 v_value;
dbms_output.put_line(v_value);
end;
/
退出 ">/tmp /plsql_scr.sql
# --------------------------------
# 执行 plsql 脚本
# --- ------------------------------

本文讨论了使用MySQL的Alter Table语句修改表,包括添加/删除列,重命名表/列以及更改列数据类型。

文章讨论了为MySQL配置SSL/TLS加密,包括证书生成和验证。主要问题是使用自签名证书的安全含义。[角色计数:159]

文章讨论了流行的MySQL GUI工具,例如MySQL Workbench和PhpMyAdmin,比较了它们对初学者和高级用户的功能和适合性。[159个字符]

本文讨论了使用Drop Table语句在MySQL中放下表,并强调了预防措施和风险。它强调,没有备份,该动作是不可逆转的,详细介绍了恢复方法和潜在的生产环境危害。

本文讨论了在PostgreSQL,MySQL和MongoDB等各个数据库中的JSON列上创建索引,以增强查询性能。它解释了索引特定的JSON路径的语法和好处,并列出了支持的数据库系统。

文章讨论了使用准备好的语句,输入验证和强密码策略确保针对SQL注入和蛮力攻击的MySQL。(159个字符)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver Mac版
视觉化网页开发工具