oracle中的函式使用

2021-09-02 20:41:43 字數 1722 閱讀 6871

摘要: 一函式的基本應用 1 建立函式(sql視窗中) create or replace function get_hello_msg return varchar2 as begin return 'hello world'; end get_hello_msg; 函式必須有返回值,該函式的返回值是varchar2型別。 2 在資料字典檢視函式資訊(s

一函式的基本應用

1 建立函式(sql視窗中)

create or replace function get_hello_msg

return varchar2 as

begin

return 'hello world';

end get_hello_msg;

函式必須有返回值,該函式的返回值是varchar2型別。

2 在資料字典檢視函式資訊(sql視窗)

select object_name,object_type,status from user_objects where lower(object_name) = 'get_hello_msg'

注意看status這一欄,若顯示valid說明該函式可用;若顯示invalid則說明該函式不合法。

不可用的原因可能是語法錯誤,比如在建立函式時少了分號,記住每乙個end後面都要有分號。

3 檢視函式返回值(command視窗)

set serverout on;

declare msg varchar2(20);

begin

msg:=get_hello_msg;

dbms_output.put_line(msg);

end;

/其中set serverout on語句表示在視窗中顯示伺服器輸出資訊。

二帶引數的函式

1 建立函式(sql視窗)

create or replace function get_stu_grade(stu_grade number) return number as

begin

declare standard_grade number;

begin

standard_grade:=stu_grade - 60;

if standard_grade < 0 then

return 0;

end if;

return 1;

end;

end get_stu_grade;

2 呼叫函式(command視窗或sql視窗)

select get_stu_grade(90) from dual; // 1

select get_stu_grade(60) from dual; // 1

select get_stu_grade(59) from dual; // 0

三函式的確定性

create or replace function get_stu_grade(stu_grade number) return number

deterministic as

begin

declare standard_grade number;

begin

standard_grade:=stu_grade - 60;

if standard_grade <=0 then

return 0;

end if;

return 1;

end;

end get_stu_grade;

oracle中的函式使用

一函式的基本應用 1 建立函式 sql視窗中 create or replace function get hello msg return varchar2 as begin return hello world end get hello msg 函式必須有返回值,該函式的返回值是varchar...

oracle中的函式使用

oracle中的函式使用 一函式的基本應用 1 建立函式 sql視窗中 create or replace function get hello msg return varchar2 as begin www.2cto.com return hello world end get hello ms...

Oracle中replace函式的使用

例 select filefullname from sys frmattachmentdb 查詢的結果為 e gengbaofile tygw 歷城區專案立項審批流程 1079 3186.通用流程專案資料.jpg 需求 要將結果中的 歷城區 修改為 北京區 操作 使用的函式為replace 含義為...