Oracle使用臨時變數

來源:互聯網
上載者:User

Oracle使用臨時變數

在Oracle資料庫中,可以使用變數來編寫通用的sql語句,在運行sql語句時,為變數輸入值,就會在sql語句中將變數替換成這些值。

臨時變數只在使用它的sql語句中有效,變數值不能保留,臨時變數也稱為替換變數。在sql語句中,如果在某個變數前面使用了&符號,那麼久表示該變數是一個臨時變數,執行sql語句時,系統會提示使用者為該變數提供一個具體的資料。

例如,在sql*plus中執行以下的命令:
SQL> select * from dept where deptno>&temp;
輸入 temp 的值:  30
原值    1: select * from dept where deptno>&temp
新值    1: select * from dept where deptno>30
    DEPTNO DNAME          LOC
---------- -------------- -------------
        40 OPERATIONS    BOSTON
SQL>
也可以使用多個的臨時變數,案例如下:


SQL> select &column_name,dname,loc from dept where &column_name>20;
輸入 column_name 的值:  deptno
輸入 column_name 的值:  deptno
原值    1: select &column_name,dname,loc from dept where &column_name>20
新值    1: select deptno,dname,loc from dept where deptno>20


    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO
        40 OPERATIONS    BOSTON
在sql語句中,如果希望重新使用某個變數並且不希望重新提示輸入,可以使用&&符號來定義臨時變數。如下:
SQL> select &&column_name,dname,loc from dept where &&column_name>10;
輸入 column_name 的值:  deptno
原值    1: select &&column_name,dname,loc from dept where &&column_name>10
新值    1: select deptno,dname,loc from dept where deptno>10


    DEPTNO DNAME          LOC
---------- -------------- -------------
        20 RESEARCH      DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS    BOSTON

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.