日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

oracle创建函数语句,Oracle 创建函数与存储过程语句积累

發(fā)布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle创建函数语句,Oracle 创建函数与存储过程语句积累 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.

創(chuàng)建一個返回字符串的函數(shù)

create or replace function get_hello_msg

return varchar2 as

begin

return 'hello world';

end get_hello_msg;

查看函數(shù)的類型和狀態(tài)

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

user_source用于查詢數(shù)據(jù)庫中定義的函數(shù)和存儲過程的代碼

select name,type,line,text from user_source where lower(name)='get_hello_msg';

select get_hello_msg() from dual;

2. 創(chuàng)建一個獲得稅收的函數(shù)

create or replace

function get_tax(p_salary number)

return number as

begin

declare tax_salary number;

begin

tax_salary := p_salary - 2000;

if tax_salary<=0 then

return 0;

end if;

if tax_salary<=500 then

return tax_salary*5/100;

end if;

if tax_salary<=2000 then

return tax_salary*10/100 - 25;

end if;

if tax_salary<=5000 then

return tax_salary*15/100 - 125;

end if;

if tax_salary<=20000 then

return tax_salary*20/100 - 375;

end if;

if tax_salary<= 40000 then

return tax_salary*25/100 - 1375;

end if;

if tax_salary<= 60000 then

return tax_salary*30/100 - 3375;

end if;

end;

end get_tax;

函數(shù)的調(diào)用:

select get_tax(6000) tax from dual;

3:一個表中含有學生姓名字段,為了獲得所有學生姓名,必須要對數(shù)據(jù)表中的數(shù)據(jù)循環(huán)處理,以獲得每個學生的姓名,并將所有學生姓名

的字符串串聯(lián)起來,可以創(chuàng)建一個函數(shù)來處理該過程。

create or replace

function get_student_string

return varchar2

as

begin

declare cursor cu_student is

select student_name from students

order by student_id;

student_nane varchar2(10);

rowString varchar2(500);

begin

open cu_student;

fetch cu_student into student_name; //將游標所指向的當前記錄的數(shù)據(jù)賦值給student_name

while cu_student%found loop //用于循環(huán)處理游標所指向的記錄

rowString:=rowString || student_name || ', ';

fetch cu_student into student_name;

end loop;

return substr(rowString, 1, length(rowString) - 1);

end;

end get_student_string;

4:存儲過程

create or replace procedure update_students

as

begin

update students set student_age=10;

commit;

end update_students;

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

select * from user_source where lower(name)='update_students';

execute update_students;

5:存儲過程-in參數(shù)

create or replace

procedure update_students(in_age in number) as

begin

update student set student_age=in_age;

commit;

end update_students;

6:存儲過程-out參數(shù)

create or replace

procedure update_students(in_age in number, out_age out number) as

begin

update students set student_age=in_age;

select student_age into out_age from students where student_id=1;

commit;

end update_students;7:存儲過程的參數(shù)---IN ? OUT ?參數(shù)

利用IN OUT參數(shù)來交換兩個變量的值

create or replace procedure swap(in_out_param1 in out number, in_out_param2 in out number)

as

begin

declare param number;

begin

param:=in_out_param1;

in_out_param1 := in_out_param2;

in_out_param2 := param;

end;

end;

8: 以視圖user_objects中的數(shù)據(jù)為例子,將其作為源表,創(chuàng)建新表target

create table target (object_id number, object_name varchar2(30), object_type varchar2(30), previous_name varchar2(30),

status varchar2(30));值得注意的是,列previous_name是指,所有記錄按照Object_id進行升序排列,處于當前記錄之前的那條記錄的object_name的列值。

利用存儲過程來解決這個問題。

create or replace procedure insert_objects as

begin

declare

cursor cu_objects is

select * from user_objects order by object_id;

obj user_objects%rowtype;

previous_name user_objects.object_name%type;

begin

open cu_objects;

fetch cu_objects into obj;

while cu_objects%found loop

insert into target

values

(

obj.object_id,

obj.object_name,

obj.object_type,

previous_name,

obj.status

);

previous_name := obj.object_name;

fetch cu_objects into obj;

end loop;

end;

end insert_objects;

總結(jié)

以上是生活随笔為你收集整理的oracle创建函数语句,Oracle 创建函数与存储过程语句积累的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。