1)define、variable用于sqlplus中,在整个sqlplus连接中都生效,而declare用于pl/sql中。
SQL> show user
USER is "TEST"
SQL> var
SP2-0568: No bind variables declared.
SQL> define
DEFINE _DATE = "20-9月 -10" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "orcl" (CHAR)
DEFINE _USER = "TEST" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000400" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1002000400" (CHAR)
SQL> var x number; ——定义变量x,类型是number
SQL> exec :x:=1; ——给变量赋值1
PL/SQL procedure successfully completed.
SQL> print x
X
----------
1
SQL> define y='10';
SQL> define
DEFINE _DATE = "20-9月 -10" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "orcl" (CHAR)
DEFINE _USER = "TEST" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000400" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1002000400" (CHAR)
DEFINE Y = "10" (CHAR)
SQL> disconn
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> print x
SP2-0640: Not connected
SP2-0641: "PRINT" requires connection to server
SQL> var x
variable x
datatype NUMBER
x变量值没了,但是变量定义让然存在
SQL> define
DEFINE _DATE = "20-9月 -10" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "" (CHAR)
DEFINE _USER = "" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000400" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1002000400" (CHAR)
DEFINE Y = "10" (CHAR)
define定义的y值仍然存在
SQL> show serveroutput
serveroutput OFF
SQL> set serveroutput on
SQL> declare
2 m number :=1;
3 n number;
4 begin
5 select n1 into n from test where rownum=m;
6 dbms_output.put_line(n);
7 end;
8 /
1
PL/SQL procedure successfully completed.
2)variable(var)和define区别在于,前者用于绑定变量,后者是用于&或&&进行变量替换。
SQL> var
SP2-0568: No bind variables declared.
SQL> var x number;
SQL> exec :x:=1;
PL/SQL procedure successfully completed.
SQL> select * from test where rownum=:x;
N1 N2
---------- ----------
1 2
SQL> define rn=1;
SQL> define
DEFINE _DATE = "20-9月 -10" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "orcl" (CHAR)
DEFINE _USER = "TEST" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000400" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE = "1002000400" (CHAR)
DEFINE RN = "1" (CHAR)
SQL> select * from test where rownum=&rn;
old 1: select * from test where rownum=&rn
new 1: select * from test where rownum=1
N1 N2
---------- ----------
1 2