精通Oracle10程式設計SQL 7 編寫控制結構

2021-09-02 11:51:23 字數 3824 閱讀 1649

/*

*編寫控制結構

*/ --條件分支語句

--簡單條件判斷

declare

v_sal number(6,2);

begin

select sal into v_sal from emp

where lower(ename)=lower('&name');

if v_sal<2000 then

update emp set sal=v_sal+200

where lower(ename)=lower('&name');

end if;

end;

select * from emp;

--二重條件分支

--如果雇員補助不是0,則在原來的基礎上增加100元,如果補助為0或null時,則設定其補助為200元

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;

--多重條件分支

declare

v_job varchar2(10);

v_sal number(6,2);

begin

select job,sal into v_job,v_sal from emp where empno=&no;

if upper(v_job)=upper('president') then

update emp set sal=v_sal+1000 where empno=&no;

elsif upper(v_job)=upper('manager') then

update emp set sal=v_sal+500 where empno=&no;

else

update emp set sal=v_sal+200 where empno=&no;

end if;

end;

select * from emp;

--case語句

--在case語句中使用單一選擇符進行等值比較

declare

v_deptno emp.deptno%type;

begin

v_deptno:=&no;

case v_deptno

when 1 then

update emp set comm=100 where deptno=v_deptno;

when 2 then

update emp set comm=80 where deptno=v_deptno;

when 3 then

update emp set comm=50 where deptno=v_deptno;

else

dbms_output.put_line('不存在該部門');

end case;

end;

select * from emp;

--在case語句中使用多種條件比較

declare

v_sal emp.sal%type;

v_ename emp.ename%type;

begin

select ename,sal into v_ename,v_sal from emp where empno=&no;

case

when v_sal<1000 then

update emp set comm=100 where ename=v_ename;

when v_sal<2000 then

update emp set comm=90 where ename=v_ename;

when v_sal<6000 then

update emp set comm=50 where ename=v_ename;

end case;

end;

select * from emp;

--迴圈語句

--基本迴圈

create table temp(cola int);

declare

i int:=1;

begin

loop

insert into temp values(i);

exit when i=10;

i:=i+1;

end loop;

end;

select * from temp;

--while迴圈

declare

i int:=1;

begin

while i<=10 loop

insert into temp values(i);

i:=i+1;

end loop;

end;

--for迴圈

begin

for i in reverse 1..10 loop

insert into temp values(i);

end loop;

end;

--巢狀迴圈和標號

declare

result int;

begin

<>

for i in 1..100 loop

<>

for j in 1..100 loop

result:=i*j;

exit outer when result=1000;

exit when result=500;

end loop inner;

dbms_output.put_line(result);

end loop outer;

dbms_output.put_line(result);

end;

--順序控制語句

--goto

declare

i int:=1;

begin

loop

insert into temp values(i);

if i=10 then

goto end_loop;

end if;

i:=i+1;

end loop;

<>

dbms_output.put_line('迴圈結束');

end;

--null

--null語句不會執行任何操作,並且會直接將控制傳遞到下一條語句

--使用null語句的主要好處是可以提高pl/sql程式的可讀性

--如果雇員工資低於300,則將其補助設定為工資的10%,如果雇員工資高於3000,則不會執行任何操作(null)

declare

v_sal emp.sal%type;

v_ename emp.ename%type;

begin

select ename,sal into v_ename,v_sal

from emp where empno=&no;

if v_sal<300 then

update emp set comm=sal*0.1 where ename=v_ename;

else

null;

end if;

end;

select * from emp;

SUSE Linux安裝oracle10及問題解決

總結了自己在redhat5.5和suse10.1上裝oracle10g的經驗。suse10安裝了orarun的時候,會自動建oracle使用者,oinstall組和dba組,在etc profile.d 目錄下面生成oracle.sh指令碼 設定oracle環境變數 在etc sysconfig目錄...

Oracle10表刪除閃回

drop後的表被放在 站 user recyclebin 裡,而不是直接刪除掉。這樣,站裡的表資訊就可以被恢復,或徹底清除。1.通過查詢 站user recyclebin獲取被刪除的表資訊,然後使用語句 flashback table to before drop rename to 將 站裡的表恢...

oracle 10 資料庫覆蓋

同事經常發來乙個dmp檔案,要求覆蓋資料庫,我一般用下面的方法完成 首先刪除該使用者,再新建使用者,匯入資料到該使用者。1 drop user username cascade 2 新建使用者 付權 3 匯入資料 imp 今天導資料時遇到乙個很奇怪的問題,一直報 ora 01940 無法刪除當前已連...