關於取變量&和&&

declare
  v_comm number(6,2);
begin
  select comm into v_comm from emp
  where empno=&&no;
  if v_comm<>0 then
     update emp set comm=v_comm+100
     where empno=&no;
  else 
     update emp set comm=200
     where empno=&no;
  end if;
end;
 
PL/SQL上的例子,輸入僱員號,執行if...then...else,由於&&只能輸入一次值,會自動保存輸入的值,所以導致接下來不會出現要求輸入的語句,而按照第一次輸入的值直接完成執行。必須退出sqplus重新登錄之後方可撤銷&&的取值,而如果採用&no的話,則必須輸入三次值方可執行,
 
declare
  v_comm number(6,2);
  v_empno emp.empno%TYPE;
begin
  v_empno:=&no;
  select comm into v_comm from emp
  where empno=v_empno;
  if v_comm<>0 then
     update emp set comm=v_comm+100
     where empno=v_empno;
  else
     update emp set comm=200
     where empno=v_empno;
  end if;
end;
 
得益於 包袱 兄弟的幫助,此PL/SQL程序可以很好的解決問題,
另外另我明白&&只取值一次的 天高雲淡 大師的例子是:
      是用undef去除&&保存的變量
SQL> select username,password from dba_users where username=upper('&&uname');
輸入 uname 的值:  sys
原值    1: select username,password from dba_users where username=upper('&&uname')
新值    1: select username,password from dba_users where username=upper('sys')

USERNAME                       PASSWORD
------------------------------ ------------------------------
SYS

SQL> select username,password from dba_users where username=upper('&&uname');
原值    1: select username,password from dba_users where username=upper('&&uname')
新值    1: select username,password from dba_users where username=upper('sys')

USERNAME                       PASSWORD
------------------------------ ------------------------------
SYS

SQL> undef uname
SQL> select username,password from dba_users where username=upper('&&uname');
輸入 uname 的值:  sys
原值    1: select username,password from dba_users where username=upper('&&uname')
新值    1: select username,password from dba_users where username=upper('sys')

USERNAME                       PASSWORD
------------------------------ ------------------------------
SYS
 

今天看到PL/SQL的最後幾頁,發現了這個恍然大悟

 該標號類似於單個&標號。但需要注意,&標號所定義的替代變量只在當前語句中起作用;而&&標號所定義的變量會在當前SQL*Plus環境中一直生效。當第一次引用no變量時,使用&&標號需要爲其輸入數據;當第二次引用no變量時,使用&標號直接引用其原有值,而不需要輸入數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章