標籤:oracle 預存程序 遊標 觸發器
建立一個添加FOOD的預存程序
create or replace procedure add_food_pro (name in varchar,price in number,description in varchar)as begininsert into food (f_name,f_price,description)values(name,price,description);commit;end;--下面的代碼是調用預存程序beginadd_food_pro(‘糖醋魚‘,12,‘美味‘);end;
建立一個帶有輸出參數的預存程序,以a+b=c為例
create or replace procedure add_num_pro (a in int,b in int,c out int)as beginc:=a+b;end;--接下來調用預存程序declarec int;begin add_num_pro(10,20,c);dbms_output.put_line(c);end;--注意最後的dbms_output.put_line();--如果想要看到結果,需要在開啟控制台,set serveroutput on
oracle資料庫中遊標的使用
declare cursor c is select *from employees; hang employees%rowtype;begin--開啟遊標open c;loop fetch c into hang; dbms_output.put_line(‘該員工的姓名是‘||hang.first_name||‘,工資是‘||hang.salary); exit when c%notfound;end loop;--關閉遊標if c%isopen then close c;end if;end;
建立一個函數
--建立一個函數,可以去掉字串的空格create or replace function noblank1( str varchar)return varcharisbeginreturn replace(str,‘ ‘,‘‘);end;--調用函數select noblank1(‘ dfa d s a ‘) from dual;
建立一個觸發器
--建立一個觸發器create or replace trigger stu_update_tribefore update on studentfor each rowbegindbms_output.put_line(‘某條記錄已經更新,原來的資料是‘||:old.age||‘新的資料是‘||:new.age);end;--觸發觸發器update student set age=21;
本文出自 “Java大白的戰地” 部落格,請務必保留此出處http://8023java.blog.51cto.com/10117207/1669102
Oracle資料庫PL/SQL預存程序遊標觸發器