`
445822357
  • 浏览: 740903 次
文章分类
社区版块
存档分类
最新评论

oracle中new_value的使用

 
阅读更多

本人来自转载,最开始是英文文档,某人加了注释

SQL*PLUS中new_value的作用还是挺大的,多次在写脚本时用到它。使用new_value,可以方便的保存从Oracle表中选择出的数据,存为变量使用。

小结了三种使用方法,只是用到的地方不同,调用new_value的方法还是相同的:

#!/bin/sh
export ORACLE_SID=CMPR1
export ORACLE_HOME=/app/oracle/product/9205
export PATH=$ORACLE_HOME/bin:$PATH
sqlplus -s/nolog< <EOF
conn / as sysdba
 
prompt ###############定义new_value###############
column inst_num new_value ninst_num format 99999;
column inst_name new_value ninst_name format a12;
column db_name new_value ndb_name format a12;
column dbid new_value ndbid format 9999999999;
 
 
select d.dbid dbid
, d.name db_name
, i.instance_number inst_num
, i.instance_name inst_name
from v$database d,
v$instance i;
 
 
prompt ###############方法一:直接作为变量调用###############
select dbid,name from v$database where name='&ndb_name';
 
 
prompt ###############方法二:传到其它变量中调用###############
variable dbid number;
variable inst_num number;
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 ###############方法三:作为SQL脚本的变量调用###############
 
@cs.sql &ndb_name &ndbid &ninst_num
Exit
EOF
 
 
[/app/oracle/utils/scripts]$ cat cs.sql
----------可以数字1、2、3,表示调用变量的顺序
select dbid,name from v$database where name='&1';
variable dbid number;
variable inst_num number;
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;
 
----------也可以直接用new_value
variable dbid number;
variable inst_num number;
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;

这里需要注意的是,new_value只能保存一个变量值,如果选择出来的数据大于一行,则只保存最后一个:

SQL> column a new_value a_value
SQL> select a from t;
 
         A
----------
         1
         3
         2
         4
         5
         6
         7
         8
         9
 
9 rows selected.
 
SQL> create table t1(b number);
 
Table created.
 
SQL> insert into t1 values (&a_value);   --返回值多于一行,最后一个值传到变量中
old   1: insert into t1 values (&a_value)
new   1: insert into t1 values (         9)
 
1 row created.
 
SQL> commit;
 
Commit complete.
 
SQL> select * from t where a=&a_value;
old   1: select * from t where a=&a_value
new   1: select * from t where a=         9
 
         A
----------
         9
 
SQL> select * from t1;
 
         B
----------
         9
 
SQL>
分享到:
评论

相关推荐

    Oracle8i_9i数据库基础

    §15.4 在 PL/SQL 中使用 sqlcode,sqlerrm 273 第十六章 存储过程和函数 276 §16.1 引言 276 §16.2 存储过程 276 §16.2.1 创建过程 276 §16.2.2 使用过程 278 §16.2.3 开发存储过程步骤 279 §16.2.3.1 编辑...

    oracle 触发器 学习笔记

    功能: 1、 允许/限制对表的修改 2、 自动生成派生列,比如自增字段 3、 ...id on employees referencing old as old_value new as new_value for each row when (new_value.department_id&lt;&gt;80 ) begin :new_value

    oracle数据备份

    Alter table table_name add (new_colum_name datatype [default value] [not null]) 修改列类型 Alter table…modify Alter table table_name modify column_name new_datatype 修改列名 Alter table…rename Alter...

    c#中oracle的to_date函数使用方法

    例子一,获取三小时前的记录 public static DataTable ... OracleParameter[] parameters = {new OracleParameter(":gxsj", OracleType.VarChar, 10)}; parameters[0].Value = DateTime.Now.AddHours(-3).ToStrin

    oracle数据库安装

    Alter table table_name add (new_colum_name datatype [default value] [not null]) 修改列类型 Alter table…modify Alter table table_name modify column_name new_datatype 修改列名 Alter table…rename Alter...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    其一、就业面广:全球前100强企业99家都在使用ORACLE相关技术,中国政府机构,大中型企事业单位都能有ORACLE技术的工程师岗位。 其二、技术层次深:如果期望进入IT服务或者产品公司(类似毕博、DELL、IBM等),...

    Javascript new Date().valueOf()的作用与时间戳由来详解

    例如,我们可以通过这段代码输出当前时间点的时间戳 代码如下:[removed] [removed](new Date().valueOf());[removed]那么何为时间戳呢? 时间戳就是从1970年1月1日0时0分0秒到当前时间点的所有秒数。1970.1.1 0:0:0...

    Expert Oracle Exadata(Apress,2ed,2015)

    Expert Oracle Exadata, 2nd Edition opens up the internals of Oracle's Exadata platform so that you can fully benefit from the most performant and scalable database hardware appliance capable of ...

    Oracle事例

    20.oracle8中扩充了group by rollup和cube的操作。有时候省了你好多功夫的。 下面的语句可以进行总计 select region_code,count(*) from aicbs.acc_woff_notify group by rollup(region_code); &lt;2&gt; 对第1个字段...

    Oracle基础知识

    column db_block_size new_value blksz noprint select value db_block_size from v$parameter where name=\'db_block_size\'; column tablespace_name format a16; column file_name format a60; set linesize ...

    Oracle P/L SQL实现发送Email、浏览网页等网络操作功能

    ab_Inline in Boolean Default False --True将文本内容直接在邮件内容显示出来,并出现在附件中,False不显示只出现在附件中 ); -----------------自动签名的生成,签名生成显示后还出现此签名文件为附件...

    Programmer's Guide to the Oracle Precompilers

    Anyone developing new applications or converting existing applications to run in the Oracle database version 7 environment will benefit from reading this guide. Written especially for programmers, ...

    Expert.Oracle.Exadata.2nd.Edition.1430262419

    If you're new to Exadata, you'll soon learn that it embodies a change in how you think about and manage relational databases. A key part of that change lies in the concept of offloading SQL ...

    PLSQL_Developer 5.1.2 for oracle and key

    * Compiler Hints added for unused declarations, unused value assignments, comparison with NULL, function without return, and to_date without format * Code Assistant can now use original case for ...

    ORACLE OSB开发指南

    ORACLE OSB开发指南,英文版。 目录: Part I IDE Help for Oracle Service Bus 1 Introduction to Oracle Service Bus 2 Tasks Working with Projects, Folders, Resources, and Configurations .....................

    vb excel 导入 access

    操作oracle数据库的记录集 Dim rst As ADODB.Recordset CommonDialog1.ShowOpen extend_str = Right(CommonDialog1.FileName, 3) If (extend_str = "xls") Then execl_cnn.Open "Provider=Microsoft.Jet.OLE...

    Oraclet中的触发器

    REFERENCING 子句说明相关名称,在行触发器的PL/SQL块和WHEN 子句中可以使用相关名称参照当前的新、旧列值,默认的相关名称分别为OLD和NEW。触发器的PL/SQL块中应用相关名称时,必须在它们之前加冒号(:),但在WHEN...

    C#实现的数据库源代码

    OracleParameter parameter = new OracleParameter(":", oracleType); parameter.SourceColumn = columnInfo.PropertyName; parameter.Value = DBNull.Value; return parameter; } private OracleType ...

    某知名公司内部ORACLE培训资料(如果你看后觉得不行,可以损我,人格担保)

    A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table...

    Unix solaris 11.1 iso最新版本

    “The value of Oracle Solaris 11 is that it maintains all the enterprise-class features expected with a mission-critical OS, while bringing in new, innovative technologies. Forsythe has a long and ...

Global site tag (gtag.js) - Google Analytics