sqlplus變數定義, 呼叫過程

tthero00boo發表於2013-09-29
 

oracle定義變數(常量)常用:declare、define、variable

1)define、variable用於sqlplus中,在整個sqlplus連線中都生效(until exit,disc是cut down session),而declare用於pl/sql中。

2)variable(var)和define區別在於,前者用於繫結變數,後者是用於&或&&進行變數替換(使用場合,拿來當輸入引數)。


define
SQL> define x='SCOTT'
SQL> define

DEFINE _DATE           = "29-9月 -13" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "myorcl11" (CHAR)
DEFINE _USER           = "SYS" (CHAR)
DEFINE _PRIVILEGE      = "AS SYSDBA" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000100" (CHAR)
DEFINE _EDITOR         = "vim" (CHAR)
DEFINE _O_VERSION      = "Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE      = "1102000100" (CHAR)
DEFINE X               = "SCOTT" (CHAR)
SQL> select distinct owner from dba_segments where wner='&x';
old   1: select distinct owner from dba_segments where wner='&x'
new   1: select distinct owner from dba_segments where wner='SCOTT'

OWNER
------------------------------------------------------------
SCOTT

var
SQL> var y numb
Usage: VAR[IABLE] [ [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
                    VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
                    NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
                    REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
SQL> var y varchar2(20)
SQL> var

variable   y
datatype   VARCHAR2(20)
SQL> exec :y := 'SCOTT'

PL/SQL procedure successfully completed.

SQL> print :y

Y
-------
SCOTT

SQL> select distinct owner from dba_segments where owner=':y'; --wrong

no rows selected

SQL> select distinct owner from dba_segments where wner=:y;

OWNER
------------------------------------------------------------
SCOTT


SQL> variable dba varchar2(30)
SQL> exec :dba := dbms_utility.make_data_block_address(4, 20);

PL/SQL procedure successfully completed.
SQL> print dba
DBA
--------------------------------
16777236

執行儲存過程:(加樓上共4種方法)

法1
BEGIN
    getDeptCount;
END;
法2
EXEC getDeptCount    
法3
CALL  getDeptCount();
注意:

定義無參儲存過程時,儲存過程名後不能加()
在塊中或是通過EXEC呼叫儲存過程時可以省略()
通過CALL呼叫無參儲存過程必須加上()

引數只能指定型別,不能指定長度精度範圍,可以指定預設值:
varchar2(200)  ,只能用varchar2

    ...PROCEDURE add_deptno(v_deptno IN dept.deptno%TYPE,
                v_dname IN VARCHAR2,
                v_loc IN dept.loc%TYPE DEFAULT 'BEJING')...
給過程傳遞引數的方法:
    1,位置傳遞 exec add_dept(50,'SALES','BEIJING')
    2,名稱傳遞 exec add_dept(v_dname=>'SALES',v_deptno=>50,v_loc=>'BEIJING')
    3,組合傳遞 exec add_dept(50,v_loc=>'BEIJING,v_dname=>'SALES')

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28859270/viewspace-773572/,如需轉載,請註明出處,否則將追究法律責任。

相關文章