替代變數與SQL*Plus環境設定

來源:互聯網
上載者:User

1.SQL與PL/SQL代碼終止符

    SQL代碼以";"來表示代碼的終止

    PL/SQL代碼以"."來表示代碼的終止,以"/"表示代碼的執行

        scott@ORCL> select * from emp where empno=7788;

 

            EMPNO ENAME      JOB              MGR HIREDATE        SAL      COMM    DEPTNO

        ---------- ---------- --------- ---------- --------- ---------- ---------- ----------

              7788 SCOTT      ANALYST        7566 19-APR-87      3100                    20

 

        scott@ORCL> declare v_ename varchar2(10);

          2  begin

          3  select ename into v_ename from emp where empno=7788;

          4  dbms_output.put_line('Employee Name: '||v_ename);

          5  exception

          6      when no_data_found then

          7      dbms_output.put_line('There is no employee');

          8  end;

          9  .

        scott@ORCL> /

        Employee Name: SCOTT

 

        PL/SQL procedure successfully completed.

     

2.替代變數

   

    &變數名、&&變數名

    執行時,如果&變數名,沒有賦值的話,會提示輸入變數值

 

        scott@ORCL> select * from emp where empno=&no;

        Enter value for no: 7788

        old  1: select * from emp where empno=&no

        new  1: select * from emp where empno=7788

 

            EMPNO ENAME      JOB              MGR HIREDATE        SAL      COMM    DEPTNO

        ---------- ---------- --------- ---------- --------- ---------- ---------- ----------

              7788 SCOTT      ANALYST        7566 19-APR-87      3100                    20

 

        scott@ORCL> save /u01/bk/scripts/select_empno.sql

        Created file /u01/bk/scripts/select_empno.sql

        scott@ORCL> @/u01/bk/scripts/select_empno.sql

        Enter value for no: 7788

        old  1: select * from emp where empno=&no

        new  1: select * from emp where empno=7788

 

            EMPNO ENAME      JOB              MGR HIREDATE        SAL      COMM    DEPTNO

        ---------- ---------- --------- ---------- --------- ---------- ---------- ----------

              7788 SCOTT      ANALYST        7566 19-APR-87      3100                    20

 

    set verify 的使用       

        scott@ORCL> set verify off  --使用set verify off來關閉替代變數使用時的提示,即執行SQL語句前不顯示使用的變數值

        scott@ORCL> @/u01/bk/scripts/select_empno.sql

        Enter value for no: 7788

 

            EMPNO ENAME      JOB              MGR HIREDATE        SAL      COMM    DEPTNO

        ---------- ---------- --------- ---------- --------- ---------- ---------- ----------

              7788 SCOTT      ANALYST        7566 19-APR-87      3100                    20   

 

    對於字元型和日期型資料,替代變數要加單引號( ' ' ),將變數括起來

        select * from emp where job='&job'

 

    &&  對輸入變數的再次引用,同時會儲存變數的值

        scott@ORCL> set serveroutput on;

        scott@ORCL> begin

          2  dbms_output.put_line('The num is '||&&num);  --使用雙&&儲存了變數num的值

          3  dbms_output.put_line('The second num is '||&num);  --因此第二次輸出變數num也是

          4  end;

          5  /

        Enter value for num: 10

        The num is 10

        The second num is 10

   

    替代變數的設定

        set define character    --修改預設的替代變數符號為其他符號,但不能使用數字和空格

        set define on          --啟用替代變數

        set define off          --關閉替代變數

       

        scott@ORCL> set define off

        scott@ORCL> @/u01/bk/scripts/select_empno.sql

        SP2-0552: Bind variable "NO" not declared.

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.