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.