SHELL和SQL:如何从SHELL传入变量到SQL
2024-07-21 02:34:24
供稿:网友
我们经常有需要从SHELL里面传入参数,到Sqlplus里面执行命令。
比如我跑一个Top进程,看到某个进程非常消耗资源。
Code:
Oracle@main-db1$top
last pid: 4327; load averages: 1.81, 2.13, 2.11
536 PRocesses: 534 sleeping, 2 on cpu
CPU states: % idle, % user, % kernel, % iowait, % swap
Memory: 12G real, 373M free, 6853M swap in use, 7235M swap free
PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
26240 oracle 1 59 -20 5592M 5569M sleep 134:10 0.71% oracle
6219 oracle 1 59 -20 5593M 5569M sleep 36:51 0.63% oracle
5717 oracle 2 59 -20 5595M 5572M sleep 35.6H 0.48% oracle
26314 oracle 1 59 -20 5593M 5572M sleep 27.4H 0.34% oracle
5553 oracle 1 59 -20 5592M 5570M sleep 910:48 0.33% oracle
22514 oracle 1 59 -20 5592M 5569M sleep 329:32 0.31% oracle
6229 oracle 1 59 -20 5592M 5568M sleep 16:00 0.28% oracle
27995 oracle 1 59 -20 5593M 5569M sleep 7:46 0.28% oracle
17843 oracle 2 59 -20 5595M 5572M sleep 594:58 0.27% oracle
我想很快看到这个进程是什么,手工进入SQLPLUS,根据v$session, v$process一个一个join,非常麻烦。
最好能够从shell 里面直接输入spid,马上让我看到这个进程是什么,在干什么。
下面是我写的一个简单例子,
Code: [Copy to clipboard]
sqlplus /nolog <<EOF
connect / as sysdba
col machine format a30
col program format a40
set line 200
select sid,serial# ,username,osuser,machine,program,process,to_char(logon_time,'yyyy/mm/dd hh24:mi:ss')
from v/$session where paddr in
( select addr from v/$process where spid in($1));
select sql_text from v/$sqltext_with_newlines
where hash_value in
(select SQL_HASH_VALUE from v/$session where
paddr in (select addr from v/$process where spid=$1)
)
order by piece;
exit;
EOF
有了这个简单例子之后,我可以很方便,及时的看到这个进程在干什么:
Code: [Copy to clipboard]
oracle@main-db1$./whoisit.sh 26240
SQL*Plus: Release 8.1.7.0.0 - ProdUCtion on Sat Dec 14 15:19:55 2002
(c) Copyright 2000 Oracle Corporation. All rights reserved.
15:19:55 SQL> Connected.
15:19:55 SQL> 15:19:55 SQL> 15:19:55 SQL> 15:19:55 SQL> 15:19:55 2 15:19:55 3
SID SERIAL# USERNAME OSUSER MACHINE PROGRAM PROCESS TO_CHAR(LOGON_TIME,
---------- ---------- ------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- -------------------
404 62253 APP1 app1 mid1 ?
@mid1 (TNS V1-V3) 6095 2002/12/13 16:00:40
1 row selected.
Elapsed: 00:00:00.02
15:19:55 SQL> 15:19:55 SQL> 15:19:55 2 15:19:55 3 15:19:55 4 15:19:55 5 15:19:55 6
SQL_TEXT
----------------------------------------------------------------
SELECT * FROM RC_REQUESTCCPAYMENT WHERE UL_LOGINNAME = :b1 A
ND RC_POSTDATE >= TO_DATE(:b2,'YYYYMM') AND RC_POSTDATE < ADD_M
ONTHS(TO_DATE(:b2,'YYYYMM'),1) AND RC_STATUS < 3000 ORDER BY RC
_REQUESTCCPAYMENTID DESC
4 rows selected.
Elapsed: 00:00:00.01
15:19:55 SQL> 15:19:55 SQL> Disconnected from Oracle8i Enterprise Edition Release 8.1.7.2.0 - 64bit Production
With the Partitioning option
JServer Release 8.1.7.2.0 - 64bit Production