儲存函式和過程

2022-07-13 01:42:11 字數 3268 閱讀 3881

儲存過程和函式

儲存過程: 對乙個模組的封裝

函式: 功能幾乎一樣

區別:函式必須通過return 關鍵字返回乙個值

儲存過程:

不需要return返回值

引數:輸入型引數

輸出型引數

8.15

declare

c student%rowtype;

cursor c_cursor is select * from student ;

begin

open c_cursor;

loop

fetch c_cursor into c;

exit when c_cursor%notfound;

dbms_output.put_line(c.sname);

end loop;

close c_cursor;

end;

declare

i number;

a student.sname%type;--引用型別

b student%rowtype;--記錄型別

begin

i:=107;

\*select s.sname into a from student s where s.sno=i;

dbms_output.put_line('查詢結果:'||a);*\

select * into b from student s where s.sno=i;

dbms_output.put_line('查詢結果:'||b.sbirthday);

end;

--25. 關於 out 型的引數: 因為函式只能有乙個返回值, pl/sql 程式可以通過 out 型的引數實現有多個返回值

--要求: 定義乙個函式: 獲取給定部門的工資總和 和 該部門的員工總數(定義為 out 型別的引數).

--要求: 部門號定義為引數, 工資總額定義為返回值.

create or replace function sal_func1(p_id in number,total_count out number)

return number

assum_sal number(

10);

cursor sal_cursor is select sal from p_emp p where p.deptno=p.id;

begin

total_count:=0

;for c in sal_cursor loop

sum_sal:=sum_sal+c.sal;

total_count:=total_count+1

;end loop;

return sum_sal;

end;

呼叫:

sql> set

serveroutput on

sql>sql>declare

2 --local variables here

3i integer;

4a number;

5begin

6 a:= sal_func1(10

,i);

7dbms_output.put_line(i);

8end;

9 /

儲存函式:

--22.2 返回乙個"helloworld: atguigu"的字串,其中atguigu 由執行函式時輸入。

create or replace function hello_world(v_logo varchar2)

return varchar2

asbegin

return 'helloworld'||v_logo;

end;

--22.3 建立乙個儲存函式,返回當前的系統時間

create or replace function sys_date

return date

asv_date varchar2(20);

begin

v_date:=sysdate;

select sysdate into v_date from dual;

dbms_output.put_line('aaaa');

return v_date;

end;

--23. 定義帶引數的函式: 兩個數相加

create or replace function add_func(a number,b number)

return number

asc number;

begin

c:=a+b;

return c;

end;

--24. 定義乙個函式: 獲取給定部門的工資總和, 要求:部門號定義為引數, 工資總額定義為返回值. 別名不能和關鍵字重名

create or replace function sal_func(p_id number)

return number

assum_sal number(10);

cursor sal_cursor is select sal from p_emp p where p.deptno=p_id;

begin

for c in sal_cursor loop

sum_sal := sum_sal + c.sal;

end loop;

return sum_sal;

end;

--25. 關於 out 型的引數: 因為函式只能有乙個返回值, pl/sql 程式可以通過 out 型的引數實現有多個返回值

--要求: 定義乙個函式: 獲取給定部門的工資總和 和 該部門的員工總數(定義為 out 型別的引數).

--要求: 部門號定義為引數, 工資總額定義為返回值.

create or replace function sal_func1(p_id in number,total_count out number)

return number

assum_sal number(10);

cursor sal_cursor is select sal from p_emp p where p.deptno=p.id;

begin

total_count:=0;

for c in sal_cursor loop

sum_sal:=sum_sal+c.sal;

total_count:=total_count+1;

end loop;

return sum_sal;

end;

儲存過程和儲存函式

1 mysql 在操作子程式時,由於需要使用分號 所以要使用delimiter先重新定義分界符為 以下 包含的內容表示注釋 delimiter 使用delimiter 把定界符由 設定為 注意 delimiter 和 之間的空格。1 建立子程式 儲存過程和儲存函式的統稱 create procedu...

儲存過程和儲存函式

儲存過程 stored procedure 是一組為了完成特定功能的sql 語句集,經編譯後儲存在資料庫。中使用者通過指定儲存過程的名字並給出引數 如果該儲存過程帶有引數 來執行它 優點 1.儲存過程只在創造時進行編譯,以後每次執行儲存過程都不需再重新編譯,而 一般sql 語句每執行一次就編譯一次,...

儲存過程和函式

子程式 命名的pl sql塊 的各個部分 申明部分,可執行部分,異常部分 可選 子程式的分類 過程 執行某些操作 函式 執行操作並返回結果 過程引數的三種模式 in,out,in out 建立過程的語法 create or replace procedure param list is as beg...