oracle 结果集变量,SQLPLUS中使用new_value把查询结果传给变量

SQLPLUS中new_value的作用还是挺大的。利用这个倒是能解决挺多问题的。

引用这么段话:

Oracle SQL*Plus has a very useful new sub-parameter to the column parameter called new_value. The new_value directive allows data that has been retrieved from an Oracle table to be stored as a variable inside the SQL*Plus script.

By using the new_value parameter you can make your SQL*Plus script behave like a real programming language, storing and addressing program variables, just like in PL/SQL.

The ability to store SQL*Plus variables and fill them with Oracle data is a very powerful feature and makes SQL*Plus scripts more efficient because database access is reduced.

使用方法小结一下:[@more@]

#!/bin/sh

export ORACLE_SID=CMPR1

export ORACLE_HOME=/app/oracle/product/9205

export PATH=$ORACLE_HOME/bin:$PATH

sqlplus -s/nolog <

conn / as sysdba

column inst_num new_value ninst_numformat 99999;

column inst_name new_valueninst_name format a12;

column db_namenew_value ndb_nameformat a12;

column dbidnew_value ndbidformat 9999999999;

select d.dbiddbid

, d.namedb_name

, i.instance_numberinst_num

, i.instance_name inst_name

from v$database d,

v$instance i;

prompt ###############Use new_value####################

select dbid,namefrom v$database where name='&ndb_name';

prompt ################Use variable###################

variable dbidnumber;

variable inst_numnumber;

begin

:dbid:=&ndbid;

:inst_num:=&ninst_num;

end;

/

select instance_name,instance_number from v$instance where instance_number=:inst_num;

select dbid,name from v$database where dbid=:dbid;

prompt ##############Use sql file#####################

@cs.sql &ndb_name &ndbid &ninst_num

Exit

EOF

[/app/oracle/utils/scripts]$ cat cs.sql

select dbid,namefrom v$database where name='&1';

variable dbidnumber;

variable inst_numnumber;

begin

:dbid:=&2;

:inst_num:=&3;

end;

/

select instance_name,instance_number from v$instance where instance_number=:inst_num;

select dbid,name from v$database where dbid=:dbid;

variable dbidnumber;

variable inst_numnumber;

begin

:dbid:=&ndbid;

:inst_num:=&ninst_num;

end;

/

select instance_name,instance_number from v$instance where instance_number=:inst_num;

select dbid,name from v$database where dbid=:dbid;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值