遊標,循環,觸發器....

 遊標

 

--定義一個遊標

declarecursor cur_sel

isselectename,sal from scott.emp; --給遊標關聯數據源

 

v_ename nvarchar2(64);

v_sal int ;

begin

  open cur_sel;--打開遊標(真正去執行關聯的sql語句)

 

       loop

         --讀取一行遊標,往下讀一行

         fetch cur_sel intov_ename,v_sal;

        dbms_output.put_line(v_ename||':'||v_sal);

       exitwhen cur_sel%notfound;

       endloop;

      

  close cur_sel;--關閉遊標

  end;

 

 

 

 循環語句

Loop

Exit when 條件

End loop

While 條件

Loop

End loop

 

For in 1..2

Loop

End loop;

 

索引

createindex index_lma_lmname on lma(lmname);

 

createindex 索引名 on 表名(字段名)

 

 

 

 觸發器

在進行操作之前運行:before

在進行操作之後運行:after

 

使用

foreachrow old new 纔可以使用

刪除::old.字段名

添加: :new.字段名

更新::old.字段名,:new.字段名

 

ü 刪除觸發器:droptrigger 索引名

 

 

ü 錯誤狀態: raise_application_error(-20001,’提示’)

 

 

 

 

  觸發器寫法:

createorreplacetrigger tri_table

beforeinsertordeleteorupdateon scott.emp

foreachrow

begin

     case

       when inserting then

         dbms_output.put_line('這是添加'||:new.ENAME);

       when deleting then

         dbms_output.put_line('只是刪除'||:old.ENAME);

       when updating then

        dbms_output.put_line(:old.ENAME||:old.ENAME);

        endcase;

end;

 

自增id的寫法

createorreplacetrigger tri_table_lll

beforeinsertordeleteorupdateon lll

foreachrow

begin

     case

       when inserting then

           select id_identity.nextval into :new.id from dual;

       when deleting then

         dbms_output.put_line('只是刪除'||:old.ENAME);

       when updating then

         dbms_output.put_line(''||:old.ENAME||:old.ENAME);

        endcase;

end;

l  調用自增:

insert into lll(ename,sal) values('lmy',45);要指明添加的字段

 

事物的四大特性

原子性,一致性,隔離性,持久性

 

保存點(commit執行前的定點回滾)

Savepoint 保存點名

Rollback to 保存點名

 

事物事例:銀行轉賬模擬

createorreplaceprocedure proc_zz(mone int,zname nvarchar2,xname nvarchar2)

is

pemp int:=0;

begin

     --轉出賬號

     update scott.emp set sal=sal-mone where ename=zname;

     pemp:= pemp+sql%rowcount;

    

     --轉入賬號

     update scott.emp set sal=sal+mone where ename=xname;

     pemp:=pemp+sql%rowcount;

    

    --pemp:=pemp/10;

    

     if pemp <2then

       dbms_output.put_line('轉賬失敗!');

       rollback;

       else

         dbms_output.put_line('轉賬成功!');

         commit;

      endif;

     

      exception

         whenothersthen

              begin

                   dbms_output.put_line('轉賬過程中出現問題!');

                   rollback;

              end;

end;

 

 調用存儲

begin

  proc_zz(1000,'SCOTT','KING');

end;

 

 

自定義報錯

 

--自定義異常

  declare ptemp int:=0;

  exp1 exception;

  exp2 exception;

  begin

  if ptemp=0then

    raise exp1;

   else

     raise exp2;

    endif;  

  exception

    when exp1 then

      dbms_output.put_line('出現了不可預計的錯誤哦...');

     when exp2 then

       dbms_output.put_line('出現了,傳說中的報錯了哦...');

     whenothersthen

       begin

           dbms_output.put_line('出現了一個小小的錯誤哦...');

         end;

     

 

  end;

 

Ø 表的備份(導入與導出):必須將導出的表放在oracle bin文件夾下面

u 導出

exp userid=cz/123456@orcl tables=(emp)file=E:\A.DMP

 

exp userid=登陸用戶名/密碼@orcltables=(表名(多張表用逗號隔開)) file=E:\名.DMP

 

導入

Imp userid=cz/123456@orclfile=E:\A.DMP

 

Imp userid=登陸用戶名/密碼@orclfile=E:\名.DMP

 

 

表未被刪除導入時需要接: ignore=y

 


 包頭:不是具體的實現

 

 

createorreplacepackage dmbs_outputm

is

functionput_line(p nvarchar2) returnnvarchar2;

functionput_line(p nvarchar2,p2 nvarchar2) return  nvarchar2;

end dmbs_outputm;

包體

 

createorreplacepackagebody dmbs_outputget

is

    function put_line(p nvarchar2)

    returnnvarchar2

     is

     begin

            return p;

       end;

  

      function put_line(p nvarchar2,p2 nvarchar2)

      return nvarchar2

      is

      begin  

        return p||p2;

     end;

 

end dmbs_outputget;

 

包頭與包體必須連續創建

 

發佈了32 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章