Oracle 事務、過程和函數

來源:互聯網
上載者:User
                                             Oracle 事務、過程和函數事務的定義與操作  1.事務的特點 ACID:  2.原子性(atomicity):組成交易處理的語句形成了一個邏輯單元,不能只執行其中的一部分。   3.一致性(consistency):在交易處理執行前後,資料庫是一致的(資料庫資料完整性約束)。   4.隔離性(isolcation):一個交易處理對另一個交易處理的影響。      5.持久性(durability):交易處理的效果能夠被永久儲存下來 。以下情況之一為事務的結束:  1.顯式的結束:執行了commit或是rollback;  2.隱式的提交:執行了DDL,DCL語句,或是exit退出。  3.隱式的復原:系統異常關閉,死機,斷電。Savepoint儲存點 (例子)        select * from emp        savepoint A;        insert into emp e (e.empno,e.ename,e.job,e.mgr) values (9999,'xiaoming','clerk',7902)        savepoint B        delete from emp where emp.empno=9999        savepoint C        rollback to C        rollback to B        rollback to A提交或復原前的資料狀態(在沒有上鎖的情況下)   1.改變前的資料狀態是可以恢複的   2.執行 DML 操作的使用者可以通過 SELECT 語句查詢之前的修正   3.其他使用者不能看到目前使用者所做的改變,直到目前使用者結束事務。提交後的資料狀態   1.資料的改變已經被儲存到資料庫中。   2.改變前的資料已經丟失。   3.所有使用者可以看到結果。   4.鎖被釋放, 其他使用者可以操作涉及到的資料。   5.所有儲存點被釋放。**************************************************************                    預存程序什麼是預存程序。       :預存程序一般用於執行一個指定的操作,可以將常用的特定操作封裝成過程。不接收參數的過程:(例子)create or replace procedure P1 asbegin  dbms_output.put_line('Current date is:' ||                       to_char(sysdate, 'yyyy-mm-dd'));end;begin  p1();end;結果:Current date is:2018-03-07接收輸入類型的參數:(例子)1.create or replace procedure addyonghu(stu_id   in number,                                        stu_name in nvarchar2,                                        class_id in number,                                        sex      in nvarchar2,                                        email    in nvarchar2,                                        address  in nvarchar2) as  begin    insert into student    values      (stu_id, stu_name, class_id, sex, email, address);  end;call addyonghu(1012,'邵夢佳',2,'女','smj@163.com','河南鞏義');2.create or replace procedure getemailbyid(stuid in student.stu_id%type) as  v_email student.email%type;begin  select s.email into v_email from student s where s.stu_id = stuid;  dbms_output.put_line('id號為' || stuid || '的郵箱為:' || v_email);exception  when no_data_found then    dbms_output.put_line('根據id號' || stuid || '找不到該學員');end;call getemailbyid(1012);接收輸出類型的參數:(例子)create or replace procedure getclassinfor(v_id        in student.stu_id%type,                                          class_infor out class%rowtype) as  class_infor1 class%rowtype;begin  select *    into class_infor    from class   where class.class_id =         (select s.class_id from student s where s.stu_id = v_id);  class_infor1 := class_infor;end;declare  class_information class%rowtype;begin  getclassinfor(1002, class_information);  dbms_output.put_line(class_information.class_id ||                       class_information.class_name ||                       class_information.class_teacher);end;in out參數:(例子)create or replace procedure studentinfo(student_information in out student%rowtype) asbegin  select *    into student_information    from student stu   where stu.stu_id = student_information.stu_id;end;      select * from student   declare  student_infor student%rowtype;begin  student_infor.stu_id := '1003';  studentinfo(student_infor);  dbms_output.put_line(student_infor.stu_id || student_infor.stu_name ||                       student_infor.class_id || student_infor.sex ||                       student_infor.email || student_infor.address);end;為形參傳遞變數和資料可以採用   位置傳遞       1.按位置傳遞是指在調用時按參數的排列順序依次寫出實參的名稱,將形參與實參關聯起來進行傳遞       2.在這種方法中,形參與實參的名稱是相互獨立、沒有關係的,次序才重要       3.它比按名稱傳遞方法在書寫上簡單,但如果更新了一個過程的形參的次序,則對應該過程的所有調用都必須進行相應的更新,所以會增加維護應用程式的難度   名稱傳遞       1.按名稱傳遞是指在調用時按照形參與實參的名稱寫出實參所對應的形參,將形參與實參關聯起來進行傳遞       2.在這種方法中,形參與實參的名稱是相互獨立、沒有關係的,名稱的對應關係很重要,但次序不重要       3.名稱傳遞在調用子程式時指定參數名,並使用關聯符號“=>”為其提供相應的數值或變數   組合傳遞       1.可以將按位置傳遞、按名稱傳遞兩種方法在同一調用中混合使用       2.但前面的實參必須使用按位置傳遞方法,而後面其餘的實參則可以使用按名稱傳遞的方法********************************************************************函數        :函數用於返回特定資料,如果在應用程式中經常需要通過執行SQL語句來返回特定資料,則可以基於這些操作建立特定的函數          函數和過程的結構類似,但必須有一個RETURN子句,用於返回函數值。函數說明要指定函數名、結果值的類型,以及參數類型等。無參函數樣本:(例子)create or replace function fun_getchar return nvarchar2 as begin   return '俊成你好';end;select fun_getchar from dual有參函數樣本:(例子)create or replace function fun(v_stu_id number ) return nvarchar2 as  v_stu_name student.stu_name%type; begin   select s.stu_name into v_stu_name from student s where s.stu_id=v_stu_id;   return v_stu_name;end; declarev_id number;begin  v_id:='&id';  dbms_output.put_line(fun(v_id));  end;過程與函數的比較    過程與函數有許多相同的功能及特性    都使用IN模式的參數傳入資料、OUT模式的參數返回資料    輸入參數都可以接收預設值,都可以傳值    調用時的實參都可以使用位置標記法或名稱標記法    都有聲明部分、執行部分和異常處理部分    一般而言,如果需要返回多個值或不傳回值,就使用過程    如果只需要返回一個值,就使用函數    雖然函數帶OUT模式的參數也能返回多個值,但是一般都認為這種方法屬於不好的編程習慣或風格    過程一般用於執行一個指定的動作,函數一般用於計算和返回一個值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.