Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28610
  • 博文数量: 14
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-27 04:53
文章分类

全部博文(14)

文章存档

2013年(14)

我的朋友

分类: Oracle

2013-08-17 18:11:37


设置一个反斜线作为转义字符. 它之后放置的任意字符除了给定一个特定含义以外 , 还当作一个直接量值. 
要想让反斜线作为转义字符, 使用以下 SQL*PLUS 命令: 

SQL> show escape
escape OFF

SQL> show define
define "&" (hex 26)

SQL> select 'a\&a' from dual;

Enter value for a: 1
old   1: select 'a\&a' from dual
new   1: select 'a\1' from dual

'A\&A'
------
a\1

SQL> set escape \
SQL> select 'a\&a' from dual;

'A\&A'
------
a&a



&符号

在Oracle中, &符号常用来指示一个变量. 例如, &fox 是个变量, 稍微有点不同的一种变量
类型是 &&fox 每当 &&fox 出现在Oracle脚本中时, 都会要求你为 &fox 提供一个值. 而使
用 &&fox, 只需在 &&fox 第一次出现时为它提供变量值. 

如果想将 & 符号作为一个普通字符使用, 应该关闭这个特性. 要想关闭这个特性, 可以运行
以下命令: 

set define off



set sqlcase {mixed | lower | upper}

SQL> show sqlcase
sqlcase MIXED

SQL> set sqlcase upper

这条命令导致SQL*PLUS把每条SQL命令都转换为大写字母.

如输入

select * from employees where first_name = 'john';

在该命令运行之前, SQL*PLUS环境将其修改为: 

SELECT * FROM EMPLOYEES WHERE FIRST_NAME = 'JOHN';



set scan on  告知SQL*PLUS扫描SQL语句中的变量, 该变量以一个 & 符号开始. 

set define on 允许SQL*PLUS定义变量





define 命令在脚本文件中定义一个变量: 

define my_dog = jimmy; 

当使用变量时, 要带 & 符号

accept 命令定义一个变量: 

prompt Enter the name of your dog; 
accept my_dog char;

如下例子: 

-- Prepare the SQL*Plus environment
set echo off;
set scan on;
set define on;

-- Have SQL*Plus define three SQL*Plus variables.
-- The value of one variable is set here in SQL*Plus the script file.
-- The user is asked for the value of the othe two variables.

define table = l_employees;
prompt Enter a valid Employee ID number;
accept employee_num number;
prompt Enter a valid Department code using uppercase letters;
accept depart_code char;

-- Run two SQL queries using those variables
select *
  from &table
 where employee_id = &employee_num;

select *
  from &table
 where dept_code = '&depart_code';

-- Return the SQL*Plus environment to the standard settings
undefine table;
undefine employee_num;
undefine depart_code;
set scan off;
set define off;
set echo on;








column 命令中的 new_value 使用: 

define gname=idle
column global_name new_value gname

select lower(user) || '@' || substr( global_name, 
       1, 
       decode( dot, 0, length(global_name), dot-1) ) global_name
  from (select global_name, instr(global_name,'.') dot 
          from global_name 
       );

set sqlprompt '&gname> '

如上: column global_name new_value gname 指令告诉SQL*PLUS取得global_name列中的最后一个值, 
      并将这个值赋给替换变量gname. 接下来, 我从数据库中选出global_name, 并与我的登录用户
      连接. 这样得到的SQL*PLUS提示符为: 

      ops$tkyte@ora11gr2> 












阅读(228) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~