乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      oracle 注意事項(xiàng)

       dingdang 2009-05-02
      1.在oracle中,數(shù)據(jù)表別名不能加as,如:
      select a.appname from appinfo a;-- 正確
      select a.appname from appinfo as a;-- 錯(cuò)誤
      也許,是怕和oracle中的存儲(chǔ)過(guò)程中的關(guān)鍵字as沖突的問(wèn)題吧
      2.在存儲(chǔ)過(guò)程中,select某一字段時(shí),后面必須緊跟into,如果select整個(gè)記錄,利用游標(biāo)的話就另當(dāng)別論了。
        select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正確編譯
        select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 沒(méi)有into,編譯報(bào)錯(cuò),提示:Compilation
         Error: PLS-00428: an INTO clause is expected in this SELECT statement

      3.在利用select...into...語(yǔ)法時(shí),必須先確保數(shù)據(jù)庫(kù)中有該條記錄,否則會(huì)報(bào)出"no data found"異常。
         可以在該語(yǔ)法之前,先利用select count(*) from 查看數(shù)據(jù)庫(kù)中是否存在該記錄,如果存在,再利用select...into...
      4.在存儲(chǔ)過(guò)程中,別名不能和字段名稱相同,否則雖然編譯可以通過(guò),但在運(yùn)行階段會(huì)報(bào)錯(cuò)
      select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正確運(yùn)行
      select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 運(yùn)行階段報(bào)錯(cuò),提示
      ORA-01422:exact fetch returns more than requested number of rows
      5.在存儲(chǔ)過(guò)程中,關(guān)于出現(xiàn)null的問(wèn)題
      假設(shè)有一個(gè)表A,定義如下:
      create table A(
      id
      varchar2(50) primary key not null,
      vcount
      number(8) not null,
      bid
      varchar2(50) not null -- 外鍵
      );
      如果在存儲(chǔ)過(guò)程中,使用如下語(yǔ)句:
      select sum(vcount) into fcount from A where bid='xxxxxx';
      如果A表中不存在bid="xxxxxx"的記錄,則fcount=null(即使fcount定義時(shí)設(shè)置了默認(rèn)值,如:fcount number(8):=0依然無(wú)效,fcount還是會(huì)變成null),這樣以后使用fcount時(shí)就可能有問(wèn)題,所以在這里最好先判斷一下:
      if fcount is null then
           fcount:
      =0;
      end
      if;

      這樣就一切ok了。

       

      1、創(chuàng)建存儲(chǔ)過(guò)程

      create or replace procedure test(var_name_1 in type,var_name_2 out type) as

      --聲明變量(變量名 變量類型)

      begin

      --存儲(chǔ)過(guò)程的執(zhí)行體

      end test;

      打印出輸入的時(shí)間信息

      E.g:

      create or replace procedure test(workDate in Date) is

      begin

      dbms_output.putline('The input date is:'||to_date(workDate,'yyyy-mm-dd'));

      end test;

      2、變量賦值

      變量名 := 值;

      E.g:

      create or replace procedure test(workDate in Date) is

      x number(4,2);

      begin

      x := 1;

      end test;

      3、判斷語(yǔ)句:

      if 比較式 then begin end; end if;

      E.g

      create or replace procedure test(x in number) is

      begin

               if x >0 then

                begin

               x := 0 - x;

               end;

           end if;

           if x = 0 then

              begin

               x: = 1;

           end;

           end if;

      end test;

      4、For 循環(huán)

      For ... in ... LOOP

      --執(zhí)行語(yǔ)句

      end LOOP;

      (1)循環(huán)遍歷游標(biāo)

      create or replace procedure test() as

      Cursor cursor is select name from student; name varchar(20);

      begin

      for name in cursor LOOP

      begin

      dbms_output.putline(name);

      end;

      end LOOP;

      end test;

      (2)循環(huán)遍歷數(shù)組

      create or replace procedure test(varArray in myPackage.TestArray) as

      --(輸入?yún)?shù)varArray 是自定義的數(shù)組類型,定義方式見(jiàn)標(biāo)題6)

      i number;

      begin

      i := 1; --存儲(chǔ)過(guò)程數(shù)組是起始位置是從1開(kāi)始的,與java、C、C++等語(yǔ)言不同。因?yàn)樵贠racle中本是沒(méi)有數(shù)組的概念的,數(shù)組其實(shí)就是一張

      --表(Table),每個(gè)數(shù)組元素就是表中的一個(gè)記錄,所以遍歷數(shù)組時(shí)就相當(dāng)于從表中的第一條記錄開(kāi)始遍歷

      for i in 1..varArray.count LOOP     

      dbms_output.putline('The No.'|| i || 'record in varArray is:'||varArray(i));   

      end LOOP;

      end test;

      5、While 循環(huán)

      while 條件語(yǔ)句 LOOP

      begin

      end;

      end LOOP;

      E.g

      create or replace procedure test(i in number) as

      begin

      while i < 10 LOOP

      begin    

      i:= i + 1;

      end;

      end LOOP;

      end test;

      6、數(shù)組

      首先明確一個(gè)概念:Oracle中本是沒(méi)有數(shù)組的概念的,數(shù)組其實(shí)就是一張表(Table),每個(gè)數(shù)組元素就是表中的一個(gè)記錄。

      使用數(shù)組時(shí),用戶可以使用Oracle已經(jīng)定義好的數(shù)組類型,或可根據(jù)自己的需要定義數(shù)組類型。

      (1)使用Oracle自帶的數(shù)組類型

      x array; --使用時(shí)需要需要進(jìn)行初始化

      e.g:

      create or replace procedure test(y out array) is

      x array;  

      begin

      x := new array();

      y := x;

      end test;

      (2)自定義的數(shù)組類型 (自定義數(shù)據(jù)類型時(shí),建議通過(guò)創(chuàng)建Package的方式實(shí)現(xiàn),以便于管理)

      E.g (自定義使用參見(jiàn)標(biāo)題4.2) create or replace package myPackage is

      -- Public type declarations type info is record(     name varchar(20),     y number);

      type TestArray is table of info index by binary_integer; --此處聲明了一個(gè)TestArray的類型數(shù)據(jù),其實(shí)其為一張存儲(chǔ)Info數(shù)據(jù)類型的Table而已,及TestArray 就是一張表,有兩個(gè)字段,一個(gè)是

      name,一個(gè)是y。需要注意的是此處使用了Index by binary_integer 編制該Table的索引項(xiàng),也可以不寫(xiě),直接寫(xiě)成:type TestArray is

      table of info,如果不寫(xiě)的話使用數(shù)組時(shí)就需要進(jìn)行初始化:varArray myPackage.TestArray; varArray := new myPackage.TestArray();

      end TestArray;

      7.游標(biāo)的使用 Oracle中Cursor是非常有用的,用于遍歷臨時(shí)表中的查詢結(jié)果。其相關(guān)方法和屬性也很多,現(xiàn)僅就常用的用法做一二介紹:

      (1)Cursor型游標(biāo)(不能用于參數(shù)傳遞)

      create or replace procedure test() is  

      cusor_1 Cursor is select std_name from student where ...; --Cursor的使用方式1 cursor_2 Cursor;

      begin

      select class_name into cursor_2 from class where ...; --Cursor的使用方式2

      可使用For x in cursor LOOP .... end LOOP; 來(lái)實(shí)現(xiàn)對(duì)Cursor的遍歷

      end test;

      (2)SYS_REFCURSOR型游標(biāo),該游標(biāo)是Oracle以預(yù)先定義的游標(biāo),可作出參數(shù)進(jìn)行傳遞

      create or replace procedure test(rsCursor out SYS_REFCURSOR) is

      cursor SYS_REFCURSOR; name varhcar(20);

      begin

      OPEN cursor FOR select name from student where ... --SYS_REFCURSOR只能通過(guò)OPEN方法來(lái)打開(kāi)和賦值

      LOOP

      fetch cursor into name    --SYS_REFCURSOR只能通過(guò)fetch into來(lái)打開(kāi)和遍歷 exit when cursor%NOTFOUND;              --SYS_REFCURSOR中可使用三個(gè)狀態(tài)屬性:                                         ---%NOTFOUND(未找到記錄信息) %FOUND(找到記錄信息)                                         ---%ROWCOUNT(然后當(dāng)前游標(biāo)所指向的行位置)

      dbms_output.putline(name);

      end LOOP;

      rsCursor := cursor;

      end test;

      下面寫(xiě)一個(gè)簡(jiǎn)單的例子來(lái)對(duì)以上所說(shuō)的存儲(chǔ)過(guò)程的用法做一個(gè)應(yīng)用:

      現(xiàn)假設(shè)存在兩張表,一張是學(xué)生成績(jī)表(studnet),字段為:stdId,math,article,language,music,sport,total,average,step                   一張是學(xué)生課外成績(jī)表(out_school),字段為:stdId,parctice,comment

      通過(guò)存儲(chǔ)過(guò)程自動(dòng)計(jì)算出每位學(xué)生的總成績(jī)和平均成績(jī),同時(shí),如果學(xué)生在課外課程中獲得的評(píng)價(jià)為A,就在總成績(jī)上加20分。

      create or replace procedure autocomputer(step in number) is

      rsCursor SYS_REFCURSOR;

      commentArray myPackage.myArray;

      math number;

      article number;

      language number;

      music number;

      sport number;

      total number;

      average number;

      stdId varchar(30);

      record myPackage.stdInfo;

      i number;

      begin

      i := 1;

      get_comment(commentArray); --調(diào)用名為get_comment()的存儲(chǔ)過(guò)程獲取學(xué)生課外評(píng)分信息

      OPEN rsCursor for select stdId,math,article,language,music,sport from student t where t.step = step;

      LOOP

      fetch rsCursor into stdId,math,article,language,music,sport; exit when rsCursor%NOTFOUND;

      total := math + article + language + music + sport;

      for i in 1..commentArray.count LOOP

      record := commentArray(i);    

      if stdId = record.stdId then  

      begin     

      if record.comment = 'A' then     

         begin         

      total := total + 20;   

          go to next; --使用go to跳出for循環(huán)       

      end;    

      end if;  

      end;  

      end if;

      end LOOP;

      <<continue>> average := total / 5;

      update student t set t.total=total and t.average = average where t.stdId = stdId;

      end LOOP;

      end;

      end autocomputer;

      --取得學(xué)生評(píng)論信息的存儲(chǔ)過(guò)程

      create or replace procedure get_comment(commentArray out myPackage.myArray) is

      rs SYS_REFCURSOR;

      record myPackage.stdInfo;

      stdId varchar(30);

      comment varchar(1);

      i number;

      begin

      open rs for select stdId,comment from out_school

      i := 1;

      LOOP

      fetch rs into stdId,comment; exit when rs%NOTFOUND;

      record.stdId := stdId;

      record.comment := comment;

      recommentArray(i) := record;

      i:=i + 1;

      end LOOP;

      end get_comment;

      --定義數(shù)組類型myArray

      create or replace package myPackage is begin

      type stdInfo is record(stdId varchar(30),comment varchar(1));

      type myArray is table of stdInfo index by binary_integer;

      end myPackage;

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多