1.
建立乙個返回字串的函式
create or replace function get_hello_msg
return varchar2 as
begin
return 'hello world';
end get_hello_msg;
檢視函式的型別和狀態
select object_name, object_type, status from user_objects where lower(object_name)='get_hello_msg';
user_source用於查詢資料庫中定義的函式和儲存過程的**
select name,type,line,text from user_source where lower(name)='get_hello_msg';
select get_hello_msg() from dual;
2. 建立乙個獲得稅收的函式
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;
函式的呼叫:
select get_tax(6000) tax from dual;
3:乙個表中含有學生姓名字段,為了獲得所有學生姓名,必須要對資料表中的資料迴圈處理,以獲得每個學生的姓名,並將所有學生姓名
的字串串聯起來,可以建立乙個函式來處理該過程。
create or replace
function get_student_string
return varchar2
asbegin
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; //將游標所指向的當前記錄的資料賦值給student_name
while cu_student%found loop //用於迴圈處理游標所指向的記錄
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引數
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引數
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:儲存過程的引數---in out 引數
利用in out引數來交換兩個變數的值
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中的資料為例子,將其作為源表,建立新錶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;
Oracle建立儲存過程 建立函式 建立包
一 oracle建立儲存過程 1 基本語法 create orreplace procedureupdate emp sal name inout type,name inout type,is begin endupdate emp sal 2 寫乙個簡單的例子修改emp表的ename欄位 cre...
Oracle建立儲存過程 建立函式 建立包
一 oracle建立儲存過程 1 基本語法 create or replace procedure update emp sal name in out type,name in out type,is begin end update emp sal 2 寫乙個簡單的例子修改emp表的ename欄...
Oracle 儲存過程與函式
儲存過程引數模式包括in out in out。in 預設引數模式 表示當儲存過程別呼叫時,實參值被傳遞給形參 形參起變數作用,只能讀該引數,而不能修改該引數。in模式引數可以是變數或表示式。out 表示當儲存過程被呼叫時,實參值被忽略 形參起未初始化的pl sql變數的作用,形參的初始值為null...