oracle 儲存過程和函式例子 2

2022-02-23 11:33:04 字數 4211 閱讀 1154

關於 游標 if,for 的例子 

create or replace procedure peace_if

is cursor var_c is select * from grade;

begin

for temp in var_c loop

if temp.course_name = 'os' then

dbms_output.put_line('stu_name = '||temp.stu_name);

elsif temp.course_name = 'db' then

dbms_output.put_line('db');

else

dbms_output.put_line('feng la feng la ');

end if;

end loop;

end;

---關於游標 for,case 的例子1

create or replace procedure peace_case1

is cursor var_c is select * from test_case;

begin

for temp in var_c loop

case temp.vol

when 1 then

dbms_output.put_line('haha1');

when 2 then

dbms_output.put_line('haha2');

when 3 then

dbms_output.put_line('haha3');

when 4 then

dbms_output.put_line('haha4');

else

dbms_output.put_line('qita');

end case ;

end loop;

end;

---關於游標 for,case 的例子2

create or replace procedure peace_case2

is cursor var_c is select * from test_case;

begin

for temp in var_c loop

case

when temp.vol=1 then

dbms_output.put_line('haha1');

when temp.vol=2 then

dbms_output.put_line('haha2');

when temp.vol=3 then

dbms_output.put_line('haha3');

when temp.vol=4 then

dbms_output.put_line('haha4');

else

dbms_output.put_line('qita');

end case ;

end loop;

end;

---關於for 迴圈的例子

create or replace procedure peace_for

is sum1 number :=0;

temp varchar2(500);

begin

for i in 1..9 loop

temp := '';

for j in 1 .. i

loop

sum1 := i * j;

temp := temp||to_char(i) || ' * ' ||to_char(j) ||' = ' ||to_char(sum1) ||' ';

end loop;

dbms_output.put_line(temp );

end loop;

end;

---關於 loop迴圈的例子

create or replace procedure peace_loop

is sum1 number := 0;

temp number :=0 ;

begin

loop

exit when temp >= 10 ;

sum1 := sum1+temp;

temp := temp +1;

end loop;

dbms_output.put_line(sum1 );

end;

---關於游標和loop迴圈的例子

create or replace procedure loop_cur

is stu_name varchar2(100);

course_name varchar2(100);

cursor var_cur is select * from grade ;

begin

open var_cur;

loop

fetch var_cur into stu_name,course_name;

exit when var_cur%notfound;

dbms_output.put_line(stu_name|| course_name);

end loop;

close var_cur;

end;

---關於異常處理的例子

create or replace procedure peace_exp(in1 in varchar2)

is c_n varchar2(100);

begin

select course_name into c_n from grade where stu_name = in1;

dbms_output.put_line(c_n);

exception

when no_data_found

then

dbms_output.put_line('try');

when too_many_rows

then

dbms_output.put_line('more');

end;

---關於異常處理的例子2

create or replace procedure peace_insert ( c_n in varchar2)

is error exception;

begin

if c_n = 'ok'

then

insert into course (course_name) values (c_n);

elsif c_n = 'ng' then

insert into course (course_name) values (c_n);

raise error;

else

dbms_output.put_line('c_n' || c_n);

end if;

commit;

exception

when error then

rollback;

dbms_output.put_line('erro');

end;

---關於包的例子 定義包

create or replace package peace_pkg

as function test1(in1 in varchar2)

return number;

procedure test2 (in2 in varchar2);

end peace_pkg;

---關於包的例子 定義包體

create or replace package body peace_pkg

as function test1(in1 in varchar2)

return number

as temp number;

begin

temp := 0;

return temp;

end;

procedure test2 (in2 in varchar2)

is begin

dbms_output.put_line(in2);

end;

end peace_pkg;

字元函式例子

要查詢以某個字結尾等於什麼的記錄 select from vehicle 車牌號以7結尾的資料記錄 select from vehicle t where trim t.veh no like 7 用函式的方式 select from vehicle t where substr trim t.ve...

oracle 儲存過程例子

oracle 儲存過程學習過程 建立乙個最簡單的儲存過程 create or replace procedure test xg p1 is begin dbms output.put line hello world this is the first procedure end 建立乙個帶輸入輸...

oracle判斷日期函式 儲存過程例子

一 判斷日期函式例子 sql create or replace function is date parmin varchar2 2 return number 3 is 4 val date 5 begin 6 val to date nvl parmin,a yyyy mm dd hh24 m...