shell sqlplus 函数的编写

阅读更多

sqlplus执行文件:sqlplus user/pass@cibsdb < sqlfile.sql   ##执行sqlfile.sql脚本


#!/bin/bash
NAME="$1"
sqlplus -S u_test/iamwangnc <select * from tab where tname = upper('$NAME');
exit
EOF


为了安全要求每次执行shell都手工输入密码

$ vi test5.sh

#!/bin/bash
echo -n "Enter password for u_test:"
read PASSWD
sqlplus -S /nolog <conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test5.sh
$ ./test5.sh

为了安全从文件读取密码

对密码文件设置权限, 只有用户自己才能读写.

$ echo 'iamwangnc' > u_test.txt
$ chmod g-rwx,o-rwx u_test.txt
$ vi test6.sh

#!/bin/bash
PASSWD=`cat u_test.txt`
sqlplus -S /nolog <conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test6.sh
$ ./test6.sh


sqlplus的结果存储在文件中

#!/bin/sh
sqlplus -S "test/unimas"<set heading off
set feedback off
set pagesize 0
set verify off
set echo off
spool spool_file
SELECT * from teacher;
spool off
exit;
EOF

或者
spoolSql(){
sql=$1;
filepath=$2;
sqlplus -S "test/unimas"< set heading off
set feedback off
set pagesize 0
set verify off
set echo off
spool filepath
$sql;
spool off
exit;
EOF
}
spoolSql "select * from sys_info" "sys_info.txt"


与下列代码效果是相同的:
#!/bin/sh
sqlplus -S "test/unimas"< spool_file
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
SELECT * from teacher;
exit;
EOF
--End--



oracle demo1:
sqlfun(){
sql=$1;
info=`sqlplus -S /nolog < set linesize 8000
set heading off
set feedback off
conn ${username}/${password}@${dbname}
set pagesize 0
set verify off
set echo off
${sql};
exit
!`
echo $info
}

使用:
cou=`sqlfun "select count(1) from sys_info"`


oracle demo2:
spoolfun(){
sql=$1;
filepath=$2;
sqlplus -S /nolog < set heading off
set echo off
set feedback off
set page 0
set linesize 32766
set trimout on
set frimspool on
set newpage none
set sqlblanklines off
set trims on
set timing off
set serveroutput off
set verify off
conn ${username}/${password}@${dbname}
${sql};
exit
!
}

使用:
spoolfun "select * from sys_info" >> "sys_info.txt"
while read sys_no sys_name
do
echo "$sys_no $sys_name"
done < sys_info.txt



informix demo3:
ifxSqlfun(){
sql=$1;
presql='SET LOCK MODE TO WAIT 10;output to pipe "cat" without headings' ;
sql=$presql$sql;
echo "$sql"|dbaccess $ifxdbname 2 >/dev/null;
}

使用方法:
sys_no_list=`ifxSqlfun "select sys_no from systeminfo"`
sys_no_str=`echo ${sys_no_list} |sed 's/ /,/g'`




informix demo4:
ifxISqlfun(){
sql=$1;
echo "$sql"|dbaccess $ifxdbname 2 >/dev/null;
}

使用:
ifxSqlfun "update sys_info set sys_name='zgs' where sys_no='00'"
sys_name=`ifxSqlfun "select sys_name from sys_info"

你可能感兴趣的:(脚本,shell,sqlplus)