文章詳情頁(yè)
ORACLE PL/SQL 基礎(chǔ)2 (游標(biāo)的學(xué)習(xí))
瀏覽:4日期:2023-11-15 11:54:19
游標(biāo)學(xué)習(xí)一> 游標(biāo)是什么: 游標(biāo)字面理解就是游動(dòng)的光標(biāo)。 用數(shù)據(jù)庫(kù)語(yǔ)言來(lái)描述:游標(biāo)是映射在結(jié)果集中一行數(shù)據(jù)上的位置實(shí)體,有了游標(biāo) ; 用戶就可以訪問(wèn)結(jié)果集中的任意一行數(shù)據(jù)了,將游標(biāo)放置到某行后,即可對(duì)該行數(shù)據(jù)進(jìn)行操作,例如提取當(dāng)前 行的數(shù)據(jù)等等。二> 游標(biāo)的分類: 顯式游標(biāo)和隱式游標(biāo) 顯示游標(biāo)的使用需要4步: 1.聲明游標(biāo) CURSOR mycur(vartype number) is select emp_no,emp_zc from cus_emp_basic where com_no = vartype; 2.打開(kāi)游標(biāo) open mycur(000627) 注:000627:參數(shù) 3.讀取數(shù)據(jù) fetch mycur into varno,varprice; 4.關(guān)閉游標(biāo) close mycur;三> 游標(biāo)的屬性 Oracle 游標(biāo)有4個(gè)屬性: %ISOPEN , %FOUND , %NOTFOUND, %ROWCOUNT %ISOPEN 判定游標(biāo)是否被打開(kāi),假如打開(kāi)%ISOPEN 等于true,否則等于false %FOUND; %NOTFOUND 判定游標(biāo)所在的行是否有效,假如有效,則%FOUNDD等于true,否則等于false %ROWCOUNT 返回當(dāng)前位置為止游標(biāo)讀取的記錄行數(shù)。四> 示例: set serveroutput on;declare varno varchar2(20); varprice varchar2(20); CURSOR mycur(vartype number) is select emp_no,emp_zc from cus_emp_basic where com_no = vartype; begin if mycur%isopen = false then open mycur(000627); end if; fetch mycur into varno,varprice; while mycur%found loop dbms_output.put_line(varno','varprice); if mycur%rowcount=2 then exit; end if; fetch mycur into varno,varprice; end loop; close mycur; end; pl/sql 記錄 的結(jié)構(gòu)和c語(yǔ)言中的結(jié)構(gòu)體類似,是由一組數(shù)據(jù)項(xiàng)構(gòu)成的邏輯單元。 pl/sql 記錄并不保存再數(shù)據(jù)庫(kù)中,它與變量一樣,保存再內(nèi)存空間中,在使用記錄時(shí)候,要首先定義記錄結(jié)構(gòu) ,然后聲明記錄變量。可以把pl/sql記錄看作是一個(gè)用戶自定義的數(shù)據(jù)類型。 set serveroutput on; declare type person is record ( empno; cus_emp_basic.emp_no%type, empzc; cus_emp_basic.emp_zc%type); person1 person; cursor mycur(vartype number)is select emp_no,emp_zc from cus_emp_basic where com_no=vartype; begin if mycur%isopen = false then open mycur(000627); end if; loop fetch mycur into person1; exit when mycur%notfound; dbms_output.put_line('雇員編號(hào):'person1.empno',地址:'person1.empzc); end loop; close mycur; end; 典型游標(biāo)for 循環(huán) 游標(biāo)for循環(huán)示顯示游標(biāo)的一種快捷使用方式,它使用for循環(huán)依次讀取結(jié)果集中的行 數(shù)據(jù),當(dāng)form循環(huán)開(kāi)始時(shí),游標(biāo)自動(dòng)打開(kāi)(不需要open),每循環(huán)一次系統(tǒng)自動(dòng)讀取 游標(biāo)當(dāng)前行的數(shù)據(jù)(不需要fetch),當(dāng)退出for循環(huán)時(shí),游標(biāo)被自動(dòng)關(guān)閉(不需要使用close) 使用游標(biāo)for循環(huán)的時(shí)候不能使用open語(yǔ)句,fetch語(yǔ)句和close語(yǔ)句,否則會(huì)產(chǎn)生錯(cuò)誤。 set serveroutput on; declare cursor mycur(vartype number)is select emp_no,emp_zc from cus_emp_basic where com_no=vartype; begin for; person in mycur(000627) loop dbms_output.put_line('雇員編號(hào):'person.emp_no',地址:'person.emp_zc); end loop; end;
標(biāo)簽:
Oracle
數(shù)據(jù)庫(kù)
排行榜
