sqlplus中define定义的常量和variable定义的变量的区别

define相当于定义一个字符常量,通过define定义之后,在通过&或者&&引用的时候不需要输入了。oracle在执行的时候自动用值进行了替换;
variable定义的是绑定变量。

(1) define

SQL> alter session set nls_language = american;

Session altered.

SQL> define
DEFINE _DATE = "03-OCT-09" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "myoracle" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000100" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options" (CHAR)
DEFINE _O_RELEASE = "1002000100" (CHAR)
SQL>

SQL> define a
SP2-0135: symbol a is UNDEFINED
SQL> define a = 1
SQL>
SQL> define
DEFINE _DATE = "03-OCT-09" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "myoracle" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000100" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options" (CHAR)
DEFINE _O_RELEASE = "1002000100" (CHAR)
DEFINE A = "1" (CHAR)

SQL> select * from dept where deptno = &a;
old 1: select * from dept where deptno = &a
new 1: select * from dept where deptno = 1

no rows selected

SQL>
SQL> column dname new_value a
SQL> select * from dept;

DEPTNO DNAME LOC ID
---------- -------------- ------------- ----------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> define
DEFINE _DATE = "03-OCT-09" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "myoracle" (CHAR)
DEFINE _USER = "SCOTT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000100" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options" (CHAR)
DEFINE _O_RELEASE = "1002000100" (CHAR)
DEFINE A = "OPERATIONS" (CHAR)
SQL>
SQL> select * from dept where dname = &a;
old 1: select * from dept where dname = &a
new 1: select * from dept where dname = OPERATIONS
select * from dept where dname = OPERATIONS
*
ERROR at line 1:
ORA-00904: "OPERATIONS": invalid identifier

SQL> select * from dept where dname = '&a';
old 1: select * from dept where dname = '&a'
new 1: select * from dept where dname = 'OPERATIONS'

DEPTNO DNAME LOC ID
---------- -------------- ------------- ----------
40 OPERATIONS BOSTON

SQL> define a
DEFINE A = "OPERATIONS" (CHAR)
SQL>
SQL> print a
SP2-0552: Bind variable "A" not declared.

(2) variable

SQL> variable a number;
SQL> print a;

A
----------

SQL> exec :a := 10;

PL/SQL procedure successfully completed.

SQL> print a;

A
----------
10

SQL> select * from dept where deptno = :a ;

DEPTNO DNAME LOC ID
---------- -------------- ------------- ----------
10 ACCOUNTING NEW YORK
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值