天再高又怎樣,踮起腳尖就更接近陽光。—-我們共勉
1.引言
官方定義
Oracle提供把PL/SQL程式存儲在資料庫中,并可以在任何地方運作它,就是存儲過程(存儲函數)。
通俗的說:把一些操作資料庫的語句(select,update,delete等)也像表和視圖一樣存在資料庫中。
小常識
1.過程和函數統稱為PL/SQL子程式。
2.過程和函數唯一的差別:
函數總是向調用者傳回資料,過程則不需要。
hello world
函數的hello world:傳回一個“hello world”字元串。
create or replace function hello_world
return varchar2
is
begin
return '我是hello world,麼麼哒!';
end;
想看一下你是否建立成功,隻需要:
SQL>select hello_world from dual;
控制台會顯示:我是hello world,麼麼哒!
2.帶輸出參數的簡單的存儲過程
create or replace function demo1(haha varchar2)
return varchar2
is
begin
dbms_output.put_line('唐伯虎是華安');
return '你好'||haha;
end;
執行以下代碼調用函數:
SQL>select demo1(‘秋香’) from dual;
你會發現輸出如下:
結論:
雖然“唐伯虎是華安”寫在前面,但是卻在後面輸出,是以輸出順序是:
先輸出return ……然後才輸出其他的。
3.建立一個存儲函數,傳回目前系統時間
create or replace function get_date
return date
is
v_date date;
begin
v_date:=sysdate;
return v_date;
end;
執行以下代碼調用函數:
SQL>select get_date from dual;
4.定義帶參數的函數:兩個數相加
create or replace function adds(num1 number,num2 number)
return number
is
begin
return num1+num2;
end;
執行以下代碼調用函數:
SQL>select adds(1,3) from dual;