函數
定義:
CREATE [OR REPLACE] FUNCTION <過程名>[(參數列表)] RETURN 數據類型 IS
?????????[局部變量聲明]
???????? BEGIN
????????????可執行語句
??????????EXCEPTION
????????????異常處理語句
??????????END [<過程名>];
變量的類型:in 為默認類型,表示輸入; out 表示只輸出;in out 表示即輸入又輸出;
使用:
???示例:
創建函數:
create or replace function f_1(n number) return number is
? ? r emp%rowtype;
BEGIN
???? dbms_output.put_line('姓名 薪水');
???? select * into r from emp where empno=n;
???? dbms_output.put_line(r.ename||' '||r.sal);????--輸出結果,需要 set serverout on 才能顯示.
???? return r.sal;
END;
使用函數:
declare
? ? n number;
???? m number;
begin
? ? n:=&請輸入員工號;
? ? m:=f_1(n);
? ? dbms_output.put_line('m的值為 '||m);
end;
刪除函數:
? ?
DROP FUNCTION <函數名>;
?
?
第一步:創建函數,并運行
create or replace function printNameAndAge
(
??? username in nvarchar2,???? -- 用戶名?
只表示輸入
? age? in out number????????????? -- 年齡?
表示即輸入又輸出;
)
return varchar2? -- 返回的類型
is
??? temp nvarchar2(200);????? -- 聲明變量
begin
? ?
??? temp := '用戶名:'||username||'?? 年齡:'||age;
???? age := age+10;? --改變年齡的值,
??? return temp;
end;
?
第二步: 調用函數
?
方式一:
declare
? username nvarchar2(200);
? age number;
? temp nvarchar2(200);
?
begin
? username := 'yun';
? age := 25;
?
? temp := printNameAndAge(username,age); -- 調用函數
?
? dbms_output.put_line('temp=='||temp); -- 打印函數的返回值
? dbms_output.put_line('age=='||age);?? -- 打印查看函數調用后的年齡的值 因為age是 in out方式的,在函數中進行了?? --改變后,在函數外部使用時候, 是被改變后的值
end;
方式二:
select printNameAndAge('aa',66) from dual;
?
第三步: 刪除函數
drop function printNameAndAge;
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
