1.if判断:
if-then-end if:
SQL> declare 2 3 v_ename emp.ename%type; 4 v_sal emp.sal%type; 5 6 begin 7 8 select ename,sal into v_ename,v_sal from emp where empno=&# 9 dbms_output.put_line('Old salary is: '||v_sal); 10 if v_sal < 8000 then 11 update emp set sal=sal+1000 where empno=# 12 commit; 13 end if; 14 select ename,sal into v_ename,v_sal from emp where empno=# 15 dbms_output.put_line('New salary is: '||v_sal); 16 17 end;
if-then-else-end if:
SQL> declare 2 3 v_ename emp.ename%type; 4 v_sal emp.sal%type; 5 6 begin 7 8 select ename,sal into v_ename,v_sal from emp where empno=&# 9 dbms_output.put_line('Old salary is: '||v_sal); 10 if v_sal < 8000 then 11 update emp set sal=sal+1000 where empno=# 12 commit; 13 else 14 update emp set sal=sal+500 where empno=# 15 commit; 16 end if; 17 select ename,sal into v_ename,v_sal from emp where empno=# 18 dbms_output.put_line('New salary is: '||v_sal); 19 20 end;
if-then-elsif-else-end if:
SQL> declare 2 3 v_ename emp.ename%type; 4 v_sal emp.sal%type; 5 v_job emp.job%type; 6 7 begin 8 9 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&# 10 dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal); 11 if v_job='SALESMAN' then 12 update emp set sal=sal+1000 where empno=# 13 commit; 14 elsif v_job='CLERK' then 15 update emp set sal=sal+800 where empno=# 16 commit; 17 else 18 update emp set sal=sal+500 where empno=# 19 commit; 20 end if; 21 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=# 22 dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_sal); 23 24 end;
SQL> declare 2 3 v_ename emp.ename%type; 4 v_sal emp.sal%type; 5 v_job emp.job%type; 6 7 begin 8 9 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&# 10 dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal); 11 if v_job='SALESMAN' then 12 update emp set sal=sal+1000 where empno=# 13 commit; 14 elsif v_job='CLERK' then 15 update emp set sal=sal+800 where empno=# 16 commit; 17 else 18 update emp set sal=sal+500 where empno=# 19 commit; 20 end if; 21 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=# 22 dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_sal); 23 24 end;
2.case分支判断:
SQL> declare 2 v_ename emp.ename%type; 3 v_job emp.job%type; 4 v_sal emp.sal%type; 5 v_out varchar2(10); 6 7 begin 8 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&# 9 dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal); 10 11 v_out:=case v_job 12 when 'CLERK' THEN v_SAL+100 13 WHEN 'SALESMAN' THEN v_SAL+200 14 END ; 15 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=# 16 dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT); 17 dbms_output.put_line(v_out); 18 end;
SQL> declare 2 v_ename emp.ename%type; 3 v_job emp.job%type; 4 v_sal emp.sal%type; 5 v_out varchar2(10); 6 7 begin 8 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&# 9 dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal); 10 11 v_out:=case v_job 12 when 'CLERK' THEN v_SAL+100 13 WHEN 'SALESMAN' THEN v_SAL+200 14 END ; 15 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=# 16 dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT); 17 18 end;
SQL> declare 2 v_ename emp.ename%type; 3 v_job emp.job%type; 4 v_sal emp.sal%type; 5 v_out varchar2(10); 6 7 begin 8 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&# 9 dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal); 10 11 v_out:=case v_job 12 when 'CLERK' THEN v_SAL+100 13 WHEN 'SALESMAN' THEN v_SAL+200 14 END ; 15 select ename,job,sal into v_ename,v_job,v_sal from emp where empno=# 16 dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT); 17 18 end;
在sql中使用case:
SQL> select ename,job,sal, 2 CASE 3 WHEN JOB='CLERK' THEN SAL+100 4 WHEN JOB='MANAGER' THEN SAL+50 5 WHEN JOB='SALESMAN' THEN SAL+300 6 ELSE 7 SAL 8 END AS ADD_SAL 9 FROM EMP;
decode 语句:
SQL> select ename,sal,JOB, 2 decode (job ,'CLERK',SAL+100, 3 'SALESMAN',SAL+300, 4 'MANAGER',SAL+50, 5 SAL) ADD_SAL 6 FROM EMP;
3.循环语句:
案例:在表中插入数据
①loop基本循环
SQL> declare 2 v_num number :=1; 3 v_count number; 4 5 begin 6 7 loop 8 insert into t1 values (v_num,'tom'||v_num ,'ok'); 9 v_num :=v_num + 1; 10 11 exit when v_num >10; 12 13 end loop; 14 commit; 15 16 select count(*) into v_count from t1; 17 18 dbms_output.put_line('T1 rows is :'||v_count); 19 20 end;
②for 循环:
SQL> declare 2 v_count number; 3 4 begin 5 6 for i in 1..10 7 loop 8 insert into t1 values (i,'tom'||i ,'ok'); 9 end loop; 10 commit; 11 12 select count(*) into v_count from t1; 13 14 dbms_output.put_line('T1 rows is :'||v_count); 15 16 end;
③while 循环:
SQL> declare 2 v_num number := 1; 3 v_count number ; 4 5 begin 6 7 while v_num <=10 loop 8 insert into t1 values (v_num,'tom'||v_num ,'ok'); 9 v_num := v_num + 1; 10 end loop; 11 commit; 12 13 select count(*) into v_count from t1; 14 15 dbms_output.put_line('T1 rows is :'||v_count); 16 17 end;